Reputation: 119
I am working with the ASP.NET chart control and I am using the following code to databind data to the chart control. The chart has three series on it: Series1, Series2 and Series3. The problem that I am having with the code below is that it only seems to bind to the first series so that when the chart is displayed, only the first series (Series1) shows, the other two do not display. If I step through he code everything seems to be fine.
Does anyone have any suggestions as to a solution to this issue?
private void ChartData1()
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["someConnectionString"].ConnectionString;
SqlCommand cmd = null;
cmd = new SqlCommand("dbo.ChartData", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Codevalue", SqlDbType.VarChar, 12);
cmd.Parameters.Add("@filter", SqlDbType.VarChar, 50);
cmd.Parameters["@Codevalue"].Value = "JAM";
cmd.Parameters["@filter"].Value = "three_letter_code";
conn.Open();
SqlDataReader chartReader = cmd.ExecuteReader();
//Bind the data using the DataBindTable method
this.Chart1.Series["Series1"].Points.DataBindXY(chartReader, "bucketdate", chartReader, "RecordCount");
this.Chart1.Series["Series2"].Points.DataBindXY(chartReader, "bucketdate", chartReader, "AverageTurns");
this.Chart1.Series["Series3"].Points.DataBindXY(chartReader, "bucketdate", chartReader, "MovingAverageTurns");
chartReader.Close();
conn.Close();
}
}
Upvotes: 0
Views: 5713
Reputation: 124696
A reader is forward only, your code as it stands would need to iterate through the reader three times.
There are a number of options, but if you want to use DataBindXY, you will need to extract the data from the reader into a collection (e.g. using LINQ) and bind to that collection.
An alternative would be to set the XValueMember
and YValueMembers
properties on each series, then call DataBind
which would then bind all three series in one iteration through the reader.
Upvotes: 1