Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i a very new to c#

I have created a bar chart in my WinForms application , by connecting them to MSSQL stored procedure as the data source , but i needed to provide a date picker function (from and to) and when user selects the date , only that much filtered data should be populated to the chart , but whenever i click the update button , the chart is getting data additionally(see the image) instead f filtering.



my code for chart in winform:

private void GraphToprej()
            {
                SqlConnection con = new SqlConnection(cs);
                SqlCommand cmd = new SqlCommand("Toprej", con);
                cmd.Parameters.AddWithValue("@fromDate", dtfrom.Value);
                cmd.Parameters.AddWithValue("@toDate", dtto.Value);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Rejcategory.Add(dr.GetString(0));
                    RejSum.Add(dr.GetInt32(1));
                }
                ChartTopRej.Series[0].Points.DataBindXY(Rejcategory, RejSum);
                dr.Close();
                con.Close();
            }
private void datefil_Click(object sender, EventArgs e)
        {
            GraphToprej();
        }


What I have tried:

my procedure for chart:

create proc toprej
@fromDate date,
@toDate date
as
select top 10 RejectReason1 as Reason, Sum(NetWt) as Quantity
from IP_Spools
Where status = 'Reject' AND DOE between  @fromDate  and  @toDate
group by RejectReason1
order by Sum(NetWt) desc
g


result:
https://i.stack.imgur.com/vz3qH.png[^]
Posted
Updated 17-Dec-20 22:56pm

1 solution

At a guess, the Rejcategory and RejSum variables are fields of your class. You do not clear them before adding the new data.

Either make them local variables, or clear both lists before you add the new data.
 
Share this answer
 
Comments
Member 14898617 18-Dec-20 5:14am    
i didn't quite get it ..can you please show the example code?
Richard Deeming 18-Dec-20 5:17am    
private void GraphToprej()
{
    Rejcategory.Clear();
    RejSum.Clear();
    ...
TheRealSteveJudge 18-Dec-20 5:19am    
5*
Member 14898617 18-Dec-20 5:45am    
Thank you so much ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900