Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


<pre>
Line 422:
Line 423:        For i As Integer = 0 To RequestHeader.Rows.Count - 1
Line 424:            Dim row1 As GridViewRow = RequestHeader.Rows(i)
Line 425:            Dim chkdelete As CheckBox = row1.FindControl("CheckItem")
Line 426:


What I have tried:

For i As Integer = 0 To RequestHeader.Rows.Count - 1
    Dim row1 As GridViewRow = RequestHeader.Rows(i)
    Dim chkdelete As CheckBox = row1.FindControl("CheckItem")
Posted
Updated 19-Jan-21 17:14pm

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

Are you sure RequestHeader's first row is number 0 and not 1 ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
AbbyP 20-Jan-21 0:57am    
Thank you Patrice. I tried the debug and find out that the value of this line is 0 - Dim row1 As GridViewRow = RequestHeader.Rows(i)

Did you know why it did not capture the value of this line which is 3 - For i As Integer = 0 To RequestHeader.Rows.Count - 1
Patrice T 20-Jan-21 2:29am    
We have no idea of what this code snipset is doing or what is the contain of variables.
Only you can check with the debugger.
Did it help you to understand the reason of the error ?
IndexOutOfRangeException
Index was out of range. Must be non-negative and less than the size of the collection.

Error type and the contents are self-explanatory in themselves. You are trying to use/access certain index which does not exists for a given collection object.

Just look around the code line where you get this error and correct the logic that is trying to access the index which does not exist.

Details on the same:IndexOutOfRangeException Class[^]

This surely would help: Troubleshooting Exceptions: System.IndexOutOfRangeException[^]


If error is occuring at: Dim row1 As GridViewRow = RequestHeader.Rows(i) then it would mean that there is no row at index i (i being the value for which the error happens)
 
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