I am using C# to create a Windows Application using .NET Framework 4.8.
In my application, I have a chart Chart performanceChart
which I am attempting to feed data through some SQL queries.
Because of how the project I'm working on is set up, I run the method that performs the query and feeds the data to the chart for each Series
I have in the chart (in this case there are 2 series: A and B).
Here is my code for the first query:
public static void getDataA(Chart chart, string wc)
{
string commandSql = "SELECT TOP 8 Example1, Example2, Example3, Example4 FROM ***** WHERE Example5 = @wc AND Example6 = 'A' ORDER BY Example7 Desc";
SqlConnection conn = new SqlConnection("Data Source = ****; Initial Catalog = ****; Integrated Security = True");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(commandSql, conn);
cmd.Parameters.AddWithValue("wc", wc);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
chart.DataSource = ds.Tables[0];
//Mapping a field with x-value of chart
chart.Series[0].XValueMember = "Production_Hour";
//Mapping a field with y-value of Chart
chart.Series[0].YValueMembers = "Cuts";
//Bind the DataTable with Chart
chart.DataBind();
conn.Close();
}
I then call a similar method, but the SQL query has a minor change that will return entirely different records, and I seemingly add that data to the Series
in the correct location (in this case it would be in the X and Y Series
1 location, because we already did this for Series
0). Here is my code:
public static void getDataB(Chart chart, string wc)
{
string commandSql = "SELECT TOP 8 Example1, Example2, Example3, Example4 FROM ***** WHERE Example5 = @wc AND Example6 = 'B' ORDER BY Example7 Desc";
SqlConnection conn = new SqlConnection("Data Source = ****; Initial Catalog = ****; Integrated Security = True");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(commandSql, conn);
cmd.Parameters.AddWithValue("wc", wc);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
chart.DataSource = ds.Tables[0];
//Mapping a field with x-value of chart
chart.Series[1].XValueMember = "Production_Hour";
//Mapping a field with y-value of Chart
chart.Series[1].YValueMembers = "Cuts";
//Bind the DataTable with Chart
chart.DataBind();
conn.Close();
}
My question is, why is the method/query ran last populating the data for each Series
in my chart? As you can see, all the data in my chart is the same (and yes, I use SSMS to check and ensure that this isn't a coincidence that the data is the same, I can ensure that the data for each series is the data from the last SQL query performed).
I know there are 3 Series
in the above image. To get my point across I only needed to share snipped for 2 out of the 3 methods to save space/time. The point still stands: the last method running the query is that data that populates ALL Series
X and Y values in the chart.
Also, I know that these methods could probably be refactored to be the same method, perhaps just requiring an extra parameter in the method arguments. I do intend to refactor at some point, but for now my intentions are just to prevent overwriting of data.