How to Link Chart Graph With Database in Windows Forms

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.

chart graph

Step 2: Coding

Now click on the Button (Load Button). and write this code:
 
Coding 
 

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.

Run

You can go to my blog_munesh

Up Next
    Ebook Download
    View all
    Learn
    View all