Rock February 2016

C# Chart showing unnecessary changes

Unnecessary Changes in Chart(Image Link) I am working on C#,Winform.This is My Code.I am trying show by chart the number of people registered in selected date Which is working perfectly at first selection of date from dateTimePicker, But at the second selection and second time button click it showing unexpected changes(Please refer the image for perfect understanding). So I don't want this changes I want every time it user pick date he has to get perfect output of only 5 bars.How I can do that? Please Help!Thanks In Advance.

private void LoadChart_Click(object sender, EventArgs e)
    {
        string date1 = dateTimePicker1.Value.ToString("dd-MM-yyyy");
        string date2 = dateTimePicker1.Value.AddDays(1).ToString("dd-MM-yyyy");
        string date3 = dateTimePicker1.Value.AddDays(2).ToString("dd-MM-yyyy");
        string date4 = dateTimePicker1.Value.AddDays(3).ToString("dd-MM-yyyy");
        string date5 = dateTimePicker1.Value.AddDays(4).ToString("dd-MM-yyyy");

        string cmdTextDate = "SELECT * FROM AllInOneTable WHERE Date=@Date";
        OleDbCommand commandDate = new OleDbCommand(cmdTextDate, my_con);
        commandDate.CommandText = cmdTextDate;
        commandDate.Parameters.AddWithValue("@Date", date1);
        DataSet data = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(commandDate);
        da.Fill(data);
        int a = data.Tables[0].Rows.Count;

        string cmdTextDate2 = "SELECT * FROM AllInOneTable WHERE Date=@Date";
        OleDbCommand commandDate2 = new OleDbCommand(cmdTextDate2, my_con);
        commandDate2.CommandText = cmdTextDate2;
        commandDate2.Parameters.AddWithValue("@Date", date2);
        DataSet data2 = new DataSet();
        OleDbDataAdapter da2 = new OleDbDataAdapter(commandDate2);
        da2.Fill(data2);
        int b = data2.Tables[0].Rows.Count;

        string cmdTextDate3 = "SELECT * FROM AllInOneTable W        

Answers


Gabe February 2016

Try clearing the series (below) then re-add the new data afterwards. Not sure if that's 100% what you want, but seems like it.

Series.Points.Clear();
//for your code
this.chart1.Series["Series1"].Points.Clear();

Also on a side note - You rarely should call SELECT * FROM or do multiple calls to the database to retrieve information that can be done with a single query.

If you're using sql, look into GROUP BY and UNION keywords for sql queries. It'll allow you to condense multiple calls into one. Then separate it as you see fit, if you see the need. Linq will come in handy here.

This Example will show you an overview of what I'm talking about even if it's not contextually the same. It is a little more advanced however.

Post Status

Asked in February 2016
Viewed 1,669 times
Voted 11
Answered 1 times

Search




Leave an answer