locked
Error: Index out of range RRS feed

  • Question

  • Im getting the error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" when trying to open my web pageWhat I simple have is one chart on my page with data coming from a Stored procedure in my sql server Datatbase. This works fine however when i try and limit the amount of data pints to be displayed on the chart it throws up this error, the chart itself is a line graph with a value for each week so obviously after 1 year it will have 52 data points and will look terrible, that is why im trying to limit the chart to display the last 20 data points etc.

    I have posted something similar to this before and got some very good samples http://social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/thread/125c408f-8275-4f54-95b2-3580e97dc371/#f99f2e78-ed24-4812-8ed8-0ddd6afb29d7

    but I could not resolve it, Could anyone explain this error and a possible solution or better still explain how to limit data points on a chart using c#. I have my chart on my page and the code behind is as follows:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace ChartTest
    {
     public partial class LimitDataPoints : System.Web.UI.Page
     {
      protected void Page_Load(System.Object sender, System.EventArgs e)
      {
    
       List<long> week = new List<long>();
       List<long> totalsavings = new List<long>();
    
       for (int i = 0; i <= 19; i++)
       {
        Chart3.Series[0].Points.AddXY(week[i], totalsavings[i]);
       }
    
       Chart3.ChartAreas[0].AxisX.Interval = 1;
      
       if (Chart3.Series[0].Points.Count > 5)
       {
        Chart3.ChartAreas[0].AxisX.ScaleView.Position = Chart3.Series[0].Points.Count - 5;
        Chart3.ChartAreas[0].AxisX.ScaleView.Size = 5;
       }
    
    
      }
     }
    
    Would appreciate any help, as I'm a bit lost and just looking to do this anyway possible, above the "week" is obviously the week number and the "total_savings" is the value of savings.

     

     

    Wednesday, May 12, 2010 2:01 PM

Answers

  • What does stack trace show? Can you post it here?
    Javaman
    • Marked as answer by NT86 Thursday, May 13, 2010 9:50 AM
    Wednesday, May 12, 2010 3:27 PM

All replies

  • What does stack trace show? Can you post it here?
    Javaman
    • Marked as answer by NT86 Thursday, May 13, 2010 9:50 AM
    Wednesday, May 12, 2010 3:27 PM
  • at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List`1.get_Item(Int32 index) at ChartTest.LimitDataPoints.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\ntuohy\Desktop\ChartTest\ChartTest\LimitDataPoints.aspx.cs:line 22
    Thursday, May 13, 2010 8:52 AM
  • I actually solved my problem it was a small problem with loading data from my stored procedure. Thanks anyway 
    Thursday, May 13, 2010 9:50 AM
  • OK good, my next step was to ask you which of the lines above was line 22.  Because that was the line causing the problem.   The code week[i] was most likely the cause because that list had no elements in the collection.
    Javaman
    Thursday, May 13, 2010 1:54 PM
  • Exactly that was the line causing the problem, i didn't have any elements in the collection, stupid mistake by myself, thanks for the posts 
    Friday, May 14, 2010 1:08 PM