I have a question realting to binding data from a database in MSChart. I have a database that contains "Value", "Time", "ID" and "UniqueID". I want to bind the X axis to "Value" and the Y axis to "Time". I use the "ID" to identify all the data that needs to be bound to the chart control. So I query the database for "ID" 1 and retrieve say the first 10 results and bind it to the chart control. The data is bound to Series "Series1". Now I add a new Series "Series2" and I Query the database to "ID" 2 and bind the data to the chart. For somereason when I bind the second query the chart only displays the second set of data.

Could you please direct me to what I am doing wrong.

   public void connect()
{
    SqlConnection cn;
    SqlCommand myCommand;

    cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
    myCommand = new SqlCommand();

    myCommand.Connection = cn;
    myCommand.CommandText = "SELECT TOP 3 [Value],[Time] FROM [dbo].[Table] WHERE [ID] = 1";

    cn.Open();
    SqlDataReader myReader = myCommand.ExecuteReader();

    Chart1.Series["Series1"].XValueMember = "Value";
    Chart1.Series["Series1"].YValueMembers = "Time";


    Chart1.DataSource = myReader;
    Chart1.DataBind();
    cn.Close();
    Chart1.Series["Series1"].Color = System.Drawing.Color.FromArgb(0, 51, 25);
}

public void connect2()
{
    SqlConnection cn;
    SqlCommand myCommand;

    cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
    myCommand = new SqlCommand();

    myCommand.Connection = cn;
    myCommand.CommandText = "SELECT TOP 3 [Value],[Time] FROM [dbo].[Table] WHERE [ID] = 2";

    cn.Open();
    SqlDataReader myReader = myCommand.ExecuteReader();

    Chart1.Series["Series1"].XValueMember = "Value";
    Chart1.Series["Series1"].YValueMembers = "Time";


    Chart1.DataSource = myReader;
    Chart1.DataBind();
    cn.Close();
    Chart1.Series["Series1"].Color = System.Drawing.Color.FromArgb(0, 51, 25);
}


protected void Page_Load(object sender, EventArgs e)
{

}
protected void Chart1_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    connect();
    connect2();
}

Related posts

Recent Viewed