1

I am running a VB.NET program and having an error of

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

My code looks like below:

Protected Sub gvadmin_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_admin.RowCommand
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim row As GridViewRow = gv_admin.Rows(index)

    If (e.CommandName = "viewdoc") Then
        Dim revno As String = gv_admin.DataKeys(index).Values(0).ToString()
        Dim dept As String = gv_admin.DataKeys(index).Values(1).ToString()
        Dim ki As String = gv_admin.DataKeys(index).Values(2).ToString()
        Dim url As String = "ScheduleViewDoc.aspx"
        Dim s As String = "window.open('" & url & "?" & "revno=" & revno & "&eqdept=" & dept & "&ki=" & ki & "', 'popup_window', 'width=1450,height=700,left=10,top=10,resizable=no');"
        ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
        BindGrid1()
    ElseIf (e.CommandName = "viewstatus") Then
        Dim eqstatus As String = gv_admin.DataKeys(index).Values(3).ToString()
        'Dim url As String = "MasterlistViewStatus.aspx"
        'Dim s As String = "window.open('" & url & "?" & "eqstatus=" & status & "', 'popup_window', 'width=450,height=500,left=10,top=10,resizable=no');"
        'ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
        liststatus(eqstatus)
        mpstatus.Show()
        BindGrid1()

    End If
End Sub

The error --> Dim row As GridViewRow = gv_admin.Rows(index)

ADyson
  • 57,178
  • 14
  • 51
  • 63
N.I.A
  • 75
  • 1
  • 7
  • 1
    ok. So what value is `index` at that time? And how many rows do you have in the gridview? The error simply means you tried to access a row which doesn't exist. Remember the indexes are zero-based – ADyson Mar 21 '18 at 09:26
  • @ADyson they seem to be following the MDSN which is good, they probably just need to check `index >= 0` – Sasha Mar 21 '18 at 09:31
  • @Jaxi and also they should check if the index is higher than the total number of rows. But why are you telling me? Tell the OP. – ADyson Mar 21 '18 at 09:36
  • @ADyson they will read this :) The index will never be higher than the total number of rows – Sasha Mar 21 '18 at 09:42
  • @Jaxi How do you know? It's coming from a commandargument, anything could be in there, we can't see how it's created – ADyson Mar 21 '18 at 09:46
  • That's from the MDSN, it's generated from the .NET framework – Sasha Mar 21 '18 at 11:33
  • I'd recommend that you try debugging the method to see why it is that you're getting an exception that you seem to think shouldn't be possible. You could set a breakpoint at the start of the method and step through every time it's called, or you could change the settings for the IndexOutOfRange exception to break when thrown instead of break when unhandled (or remove whatever handler is catching the exception). That will let you see what value of `Index` is causing the problem, and hopefully you can figure out how it got to be bad. – Craig Mar 21 '18 at 14:10
  • I'm still new in VB.Net. I was try to do index paging and it was fine for first, second and the next paging.But when I clicked at the last paging, the error comes out. – N.I.A Mar 23 '18 at 00:27

1 Answers1

0

This code has no checks for exceptions. You should always check if the index is valid in your case. Add a check for index. I am not that good in vb. Something like:

if(index < 0) return;

Before getting the rows based on index.

Also, write a check for e.CommandArgument. It is always safer to have these checks in you code

Praneet Nadkar
  • 823
  • 7
  • 16