Rico Brouwer
Rico Brouwer

Reputation: 81

C# Chart series error

I am building my first chart in visual studio with c#. I created a chart and database and created the connection between them. The Database has two columns "DrukSensor"(float) and "DateTime" (DateTime).

In my code i want to create a chart with on the x axis the DateTime and on the Y axis the Druksensor.

But when i am trying my code it gives an error with: No Chart element found with name "Druksensor" in the seriescollection.

Tried surfing the web to find the right anwser but could not find it unfortunately.

Here is my code:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=LP12;Initial Catalog=SmmsData;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select DrukSensor,DateTime from SysteemSensorInfo2", con);

        SqlDataReader myreader;


        DataSet ds = new DataSet();
        new SqlDataAdapter(cmd).Fill(ds);
        myreader = cmd.ExecuteReader();

        Chart1.Series["DrukSensor"].Points.AddXY(myreader.GetDateTime(Int32.Parse("DateTime")), myreader.GetFloat(Int32.Parse("DrukSensor")));
        Chart1.DataSource = ds;
        Chart1.DataBind();
    }

I hope somebody can help me with my first chart build.

Thanks in advance!

Upvotes: 0

Views: 561

Answers (2)

Braindead
Braindead

Reputation: 31

Looks like you are not add the series. You can do it with:

  chart1.Series.Add("DrukSensor");

Note: remember redraw it.

chart1.Update();

Upvotes: 0

A. Mohseni Azghandi
A. Mohseni Azghandi

Reputation: 129

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=LP12;Initial Catalog=SmmsData;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select DrukSensor,DateTime from SysteemSensorInfo2", con);

        SqlDataReader myreader;


        DataSet ds = new DataSet();
        new SqlDataAdapter(cmd).Fill(ds);
        myreader = cmd.ExecuteReader();

        Chart1.Series.Add("DrukSensor"); // Add this line

        Chart1.Series["DrukSensor"].Points.AddXY(myreader.GetDateTime(Int32.Parse("DateTime")), myreader.GetFloat(Int32.Parse("DrukSensor")));
        Chart1.DataSource = ds;
        Chart1.DataBind();
    }

Upvotes: 1

Related Questions