Click here to Skip to main content
15,905,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi 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.

"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:

What I have tried:

VB
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)
Posted
Updated 25-Mar-18 20:00pm
v2
Comments
Bryian Tan 25-Mar-18 22:38pm    
Not clear what in the e.CommandArgument. I'm assumed it passing (1,2,3,45,6...)? The Rows index start at 0,1,2,3,4,5 ... try gv_admin.Rows(index-1)

1 solution

The error message is pretty explicit:
Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index
The value of index is either negative, or invalid. Remember, .NET array indexes start at zero, and go up to the number of elements minus one, so if you have three elements in your array, valid indexes will be 0, 1, and 2 - any other value will give you this exception.

You need to find out what index is, and how big the gv_admin.Rows collection is. We can't do that - we don;t have any access to your data, or your code while it's running.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, 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
 

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