0
votes

my windowsform in c# has a graph. when i enter the data for the first time, the graph works fine, and when i click clear, everything clears up. the problem is when i enter data for the 2nd time andtry to load the graph c# then gives me error.

    private void button2_Click(object sender, EventArgs e)
    {
        //Graph codes
        if (p == 1)
        {
            if (t == 1)
            {
                if (txtFirstActual.Text == "" || txtSecondActual.Text == "" || txtThirdActual.Text == "" || txtFourthActual.Text == "")
                {
                    MessageBox.Show("Enter All Value Of Actual Years");
                }
                else
                {
                    int m = Convert.ToInt32(txtForecastingYear.Text);

                    int FirstYearValue = m - 4;
                    txtFirstYear.Text = FirstYearValue.ToString();

                    int SecondYearValue = m - 3;
                    txtSecondYear.Text = SecondYearValue.ToString();

                    int ThirdYearValue = m - 2;
                    txtThirdYear.Text = ThirdYearValue.ToString();

                    int FourthYearValue = m - 1;
                    txtFourthYear.Text = FourthYearValue.ToString();

                    double FirstActual = Convert.ToDouble(txtFirstActual.Text);
                    double SecondActual = Convert.ToDouble(txtSecondActual.Text);
                    double ThirdActual = Convert.ToDouble(txtThirdActual.Text);
                    double FourthActual = Convert.ToDouble(txtFourthActual.Text);


                    if (FirstActual == 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0) //if the user entered 3 years instead of 4
                    {

                        double sum = (SecondActual + ThirdActual + FourthActual) / 3;

                        listBox1.Items.Clear();
                        listBox1.Items.Add(sum);


                        this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year", sum);
                    }
                    else if (FirstActual != 0 && SecondActual == 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 3 years instead of 4
                    {
                        double sum = (FirstActual + ThirdActual + FourthActual) / 3;

                        listBox1.Items.Clear();
                        listBox1.Items.Add(sum);

                        this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);

                        this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year", sum);
                    }
                    else if (FirstActual != 0 && SecondActual != 0 && ThirdActual == 0 && FourthActual != 0)//if the user entered 3 years instead of 4
                    {
                        double sum = (FirstActual + SecondActual + FourthActual) / 3;

                        listBox1.Items.Clear();
                        listBox1.Items.Add(sum);

                        this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);

                        this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year", sum);
                    }

                    else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual == 0)//if the user entered 3 years instead of 4
                    {

                        double sum = (FirstActual + SecondActual + ThirdActual) / 3;

                        listBox1.Items.Clear();
                        listBox1.Items.Add(sum);

                        this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);

                        this.chart1.Series["Sales"].Points.AddXY("Year", sum);
                    }

                    else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 4 years 
                    {

                        double sum = (FirstActual + SecondActual + ThirdActual + FourthActual) / 4;

                        listBox1.Items.Clear();
                        listBox1.Items.Add(sum);

                        this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
                        this.chart1.Series["Sales"].Points.AddXY("Year", sum);
                    }

                    else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 4 years 
                    {

                        double sum = (FirstActual + SecondActual + ThirdActual + FourthActual) / 4;

                        listBox1.Items.Clear();
                        listBox1.Items.Add(sum);

                        MessageBox.Show(" Can't drow graph because there are no sales");
                    }
                }
                if (t == 0)
                {
                    MessageBox.Show("Enter Actual for Years");
                }
            }
            if (p == 0)
            {
                MessageBox.Show("Enter Wanted Year");
            }

and that is my clear button: private void button1_Click(object sender, EventArgs e) { // clear button

        listBox1.Items.Clear();

        txtForecastingYear.Clear();
        txtFirstYear.Clear();
        txtSecondYear.Clear();
        txtThirdYear.Clear();
        txtFourthYear.Clear();

        chart1.Series.Clear();

        txtFirstActual.Clear();
        txtSecondActual.Clear();
        txtThirdActual.Clear();
        txtFourthActual.Clear();

        if (!String.IsNullOrEmpty(txtFirstActual.Text)) //if there is entry then make clear button available, if not the button will do nothing
        {
            // clear data
        }
2
What is the error message? Also we need to see the clearing code as well.Geeky Guy
that's what the c# say: A chart element with the name 'Sales' could not be found in the 'SeriesCollection'. and these are the clearing codes ( i've updated the post)Mohammed Turki
Are you clearing the chart1 control? or calling .Dispose()?Evan L
im using this : chart1.Series.Clear(); in the clearing button to clear the chartMohammed Turki

2 Answers

1
votes

Your code is depending on the "Sales" series, like in the following example:

this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);

But, probably when you Clear the Chart, you are also clearing the Series of the chart, so, at the second time, the chart does not recognize the series "Sales"

To correct this, you need to check if the series exists and create it if it is null. Something like this:

if (this.chart1.Series.Count == 0)
{
    // Here you need to properly create the series
    this.chart1.Series.Add(new Series("Sales"));
}

This is a generic sample and may not compile, because i dont know the type of series or wich chart control are you using. So be sure to alter accordingly.

If you dont know how to call the constructor of this series, take a loog at the Designer.cs file and copy the constructor and properties in this code and will be fine.

1
votes

It looks like your problem is that when you call chart1.Series.Clear() it clears all the indexes of chart1 and then when you click to populate the Graph again, you are never creating the "Sales" index again. You need to add the "Sales" graph to the Series again before you can call methods on it.