Here I will explain how to connect a Chart-Graph with a database.
Step 1: Form
Drag and drop a chart graph and a button from the tool box.
Step 2: Coding
Now click on the Button (Load Button). and write this code:
private void Load_button_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server = MUNESH;Database=datastore;UID=sa;Password=123;");
SqlCommand cmd = new SqlCommand("Select * from data1", con);
DataReader mydatareader ;
try
{
con.Open();
myreader = cmd.ExecuteReader();
while(myreader.Read())
{
this.chart1.Series["Age"].Points.AddXY(myreader.GetString("name"),myreader.GetInt32("Age"));
}
}
Catch(Exception ex)
{
MessageBox.show(ex.message);
}
}
Step 3: Run
Now run your application and click on the button.