Click here to Skip to main content
15,887,251 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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? I did not understand. Please help me out.

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;
       }
Posted
Updated 4-Aug-17 12:59pm
v2

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.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
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.
 
Share this answer
 
Comments
[no name] 4-Aug-17 10:49am    
Now how do I handle the problem. since my query return less row than the row in data table, for example my datatable has studentNum 1,2,3 and now query is returning grade for 1 and 2.
Richard Deeming 4-Aug-17 10:55am    
You'll need to return Student_Num from the query, and find some way to match that to the relevant grade in the array.
[no name] 4-Aug-17 11:21am    
Thank you sir
Quote:
I could not understand why it is throwing that exception.

Do not do programs relying on expectations, make your program check if expectations are met. When your program fail, use the debugger to inspect variables and check which is the unexpected condition.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
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