Click here to Skip to main content
15,921,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Whats wrong with this code why this exception occur . 

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List`1.get_Item(Int32 index) at CourseAcademics_ProgressReports.EvalResult(List`1[] a, List`1[] totalmarks, List`1 names, Int32 subjectcount, DataTable dt) in c:\Users\Administrator\Documents\Visual Studio 2010\Projects\RootsERPWeb\ERPWeb\CourseAcademics\ProgressReports.aspx.cs:line 315


C#
public void EvalResult(List<double>[] a, List<double>[] totalmarks, List<string> names, int subjectcount, DataTable dt)
    {

        dt1 = new DataTable();
        try
        {
            for (int i = 0; i < a[0].Count; i++)
            {
                string[] xxx = new string[subjectcount + 4];
                xxx[0] = names[i];
                for (int j = 1; j <= subjectcount; j++)
                {
          double perc = Math.Round((a[j - 1][i] / totalmarks[j - 1][i]) * 100, 2);
                    xxx[j] = a[j - 1][i] + "/" + totalmarks[j - 1][i];
                }
                double obtainedmarkstotal = 0;
                double totalmark = 0;
                for (int k = 0; k < subjectcount; k++)
                {

                    obtainedmarkstotal += a[k][i];
                    totalmark += totalmarks[k][i];

                }
         double perctotal = Math.Round(obtainedmarkstotal / totalmark * 100, 2);
                xxx[subjectcount + 1] = obtainedmarkstotal + "/" + totalmark;
                xxx[subjectcount + 2] = perctotal.ToString() + "%";
                xxx[subjectcount + 3] = getgrade(perctotal);
                dt.Rows.Add(xxx);
            }
      
            gvforawards.Visible = true;
            Label1.Text = "";
            dt1 =(DataTable) dt;
            Session["result"]=dt1;
           gvforawards.DataSource = dt1;
           gvforawards.DataBind();
           int s = 0;
           int columns_count = gvforawards.Columns.Count;
           foreach (GridViewRow gvr in gvforawards.Rows)
           {
((HyperLink)gvr.Cells[0].Controls[0]).NavigateUrl="~/MarkSheet.aspx?;                             
               s++;
           }

        }
        catch(Exception ex)
        {
           
            gvforawards.Visible = false;
            Label1.ForeColor = Color.Red;
            Label1.Visible = true;
            Label1.Text = ex.ToString();
        }
    }


What I have tried:

when j value is 9 then exception occur.Can anyone tell me What mistake i have Done ?
Posted
Updated 26-Dec-16 20:09pm

1 solution

We can't tell, because we can't run your code under the same circumstances you do - we don't have access to the data you pass to the method.

So it's up to you to use the debugger to look at exactly what is happening when the exception happens. Run you so in the debugger, and when the exception happens look at exactly which line is throwing the exception - it's on line 315 of you file, but we don't have any idea which one that is - and use the debugger to find out what the various variables involved contain.
You can then work back from that to find out why - but you code appears to make a lot of assumptions that the sizes of all data items are the same, and that variables outside the method contain valid data.

Sorry, but we can't do any of 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