Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am having this exception while running c# code
i do not have any idea y i am getting this error

What I have tried:

C#
private void button6_Click(object sender, EventArgs e)
       {
           MessageBox.Show("enter");
           DateTime Frm = DateTime.Now;
           DateTime To = DateTime.Now.AddDays(-7);
           double min = To.ToOADate();
           double max = Frm.ToOADate();

          // chartefficiency = new Chart();
          // ChartArea ChartArea1 = new ChartArea();
           chartefficiency.ChartAreas["ChartArea1"].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
          chartefficiency.Series[1].XValueType = ChartValueType.Date;
           //DayOfWeek ds = DayOfWeek.Wednesday;
           //double dblIntervalOffset = Convert.ToDouble(ds);
           //chartefficiency.ChartAreas["ChartArea1"].AxisX.IntervalOffset = dblIntervalOffset;
           chartefficiency.ChartAreas["ChartArea1"].AxisX.Minimum = min;
           chartefficiency.ChartAreas["ChartArea1"].AxisX.Maximum = max;
           chartefficiency.ChartAreas["ChartArea1"].AxisX.Interval = -7;
           chartefficiency.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;

       }
Posted
Updated 24-May-22 6:27am
v2
Comments
Richard MacCutchan 24-May-22 4:50am    
Which line produces the error message? Which, incidentally, is telling you that you are trying to use an index into an array, that is either negative, or higher than the highest offset. For example;
array foo = { 1, 2, 3 }; // valid indices are 0, 1, 2
bar = foo[5]; // error index too large
poo = foo[-2]; // index negative
Patrice T 24-May-22 5:01am    
And the position of error is ... (line number)
Give exact error message

Just a wild guess: you have just 1 series but the following statement
Quote:
chartefficiency.Series[1].XValueType = ChartValueType.Date;
is trying to access the second item. Replace it with
C#
chartefficiency.Series[0].XValueType = ChartValueType.Date;
 
Share this answer
 
Array indexes in C#(and most other languages) run from 0 to the number of items in the array - 1. So an integer array with three elements containing 101, 102, and 103 will have three valid indices:
arr[0] == 101
arr[1] == 102
arr[2] == 103
Any other value at all (i.e. negative or greater than 2) will be "outside the bounds" of the array - the values do not exist and cannot be accessed. Any attempt to read r write with an invalid index will throw an Index was out of range exception and your app will stop.

Since there is only one numeric index being used:
C#
chartefficiency.Series[1].XValueType = ChartValueType.Date;
That means there is only one series in the chart, and you should be accessing it like this:
C#
chartefficiency.Series[0].XValueType = ChartValueType.Date;
But you should use the debugger to confirm that is the line giving an error, and that there are any series loaded into the control!
Sorry, but we can't do that for you!
 
Share this answer
 

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