Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve index was out of range. Must be non-negative and less than the size of the collection. Error

I am getting following exceptions in my code

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I could not understand why it is throwing that exception.

Flow of program is something like this :

I have collection of number say 1 to 200. For each number in collections, it goes and run the below code. till 120, it did not throw the exception but for 121, its throwing the exception. And it does randomly, if run the program again, I might get same exception for 123.

What could be the reason? Is it something related with database connection? Is there any problem in my code? if so then should not it be throwing for the first iteration?

What I have tried:

public DataTable GetGrade(DataTable dtReportTbl)
        {
            dtReportTbl.Columns.Add("Salary_Grade", typeof(System.String));
            dtReportTbl.Columns.Add("InsertedDate", typeof(DateTime));
            string query = "select Grade from student_grade where myTable in ('JMS','JSM') and  student_Num in ({StudentNums}) order by student_Num";
 
            try
            {
                using (OracleConnection con = new OracleConnection(connectionString))
                {
                    List<string> storeNums = new List<string>();
 

                    foreach (DataRow row in dtReportTbl.Rows)
                    {
                        storeNums.Add(row["store"].ToString());
                    }
 
                    var cmd = new OracleCommand(query, con);
 
                    // extension method to get StudentNums as to achieve cmd.Parameters.Add(new OracleParameter("pStudentNums", studentNum)
                    cmd.AddArrayParametersOra(storeNums, "StudentNums");
 
                    var rdr = cmd.ExecuteReader();
                    List<string> salGradelist = new List<string>();
                    while (rdr.Read())
                    {
                        salGradelist.Add(rdr["Grade"].ToString());
                    }
 
                    int i = 0;
                    foreach (DataRow row in dtReportTbl.Rows)
                    {
                        row["Grade"] = salGradelist[i];
                        row["RecordInsertedDate"] = DateTime.Today;
                        i++;
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
 
            return dtReportTbl;
        }
like image 923
Elay Avatar asked Oct 18 '22 08:10

Elay


1 Answers

To find out, you need to look at the data in your app while it is running - and we can;t do that! So, its going to be up to you. Put a breakpoint on the first line in the method, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

The most likely cause is that the number of rows returned from your query is not the same as the number of elements in your array.

int i = 0;
foreach (DataRow row in dtReportTbl.Rows)
{
    ... = salGradelist[i]; // <-- Exception here
    ...
    i++;
}

If the query returns fewer rows than the number of elements in your array, then your code will just ignore the later elements.

But if it returns more rows, then the variable i is going to go beyond the end of your array, and you'll get the "index out of bounds" exception.

You'll need to debug your code and your query to find out why the number of rows returned doesn't match the number of elements in your array.

like image 180
Farhad Bagherlo Avatar answered Oct 21 '22 07:10

Farhad Bagherlo