miladel
Member
- Joined
- Aug 16, 2020
- Messages
- 14
- Programming Experience
- Beginner
Hi all,
I have some problems to add a chart with sql data.
to define the problem, I need to collect data from a sensor, store it in database and use it as a chart and data grid by time descending format.
the code below is a simple example part of the project, the problem is that whenever I start the application, data shows on data grid view and chart but the chart data just like a zigzag line.
every new line just a slop of Sensor's end & start values (see attached picture: here [67,43],[65,43],[63,43],...)
another problem is that no data stores in database and all fields are null.
project attached as a zip file.
thanks for helping as ever.
I have some problems to add a chart with sql data.
to define the problem, I need to collect data from a sensor, store it in database and use it as a chart and data grid by time descending format.
the code below is a simple example part of the project, the problem is that whenever I start the application, data shows on data grid view and chart but the chart data just like a zigzag line.
every new line just a slop of Sensor's end & start values (see attached picture: here [67,43],[65,43],[63,43],...)
another problem is that no data stores in database and all fields are null.
project attached as a zip file.
thanks for helping as ever.
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Web.UI.DataVisualization.Charting;
namespace p1
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd1 = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
int inputData;
public Form1()
{
InitializeComponent();
}
private void BtnStart_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Timer1_Tick(object sender, EventArgs e)
{
serialPort1.Open();
serialPort1.Write("1");
txtData.Text = serialPort1.ReadLine();
serialPort1.Close();
SqlCommand c1 = new SqlCommand();
c1.CommandText = "insert into tblCount values (@p1,@p2)";
inputData = Int32.Parse(txtData.Text);
c1.Parameters.AddWithValue("p1",DateTime.Now.ToString());
c1.Parameters.AddWithValue("p2", inputData);
c1.Connection = conn;
c1.ExecuteNonQuery();
fillgrid();
}
private void BtnStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
conn.Open();
fillgrid();
}
void fillgrid(string s="select * from tblCount")
{
cmd1.CommandText = s;
cmd1.Connection = conn;
da.SelectCommand = cmd1;
ds.Clear();
da.Fill(ds, "T1");
dataGridView1.DataBindings.Clear();
dataGridView1.DataBindings.Add("datasource", ds, "T1");
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
txtData.DataBindings.Clear();
//===================================================Chart1
SqlConnection conn2 = new SqlConnection();
conn2.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
conn2.Open();
SqlCommand cmd2 = new SqlCommand("select sensorValue from tblCount order by dateTime desc", conn2);
SqlDataReader reader = cmd2.ExecuteReader();
Series sr = new Series();
while (reader.Read())
chart1.Series[0].Points.AddY(reader.GetInt32(0));
}
}
}