错误=索引超出范围。必须是非负数且小于集合的大小。参数名称:index

时间:2018-03-21 09:20:08

标签: asp.net vb.net gridview webforms

我正在运行VB.NET程序并且出现错误

  

"指数超出范围。必须是非负的且小于   集合的大小。参数名称:index"

我的代码如下所示:

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

错误 - > Dim row As GridViewRow = gv_admin.Rows(index)

1 个答案:

答案 0 :(得分:0)

此代码没有检查异常。您应该始终检查索引是否在您的情况下有效。添加一个索引检查。我不是那么好的vb。类似的东西:

if(index < 0) return;

在根据索引获取行之前。

另外,写一张e.CommandArgument的支票。在代码中进行这些检查总是更安全