vb.net 如何解决“错误:索引超出范围。必须为非负且小于集合的大小。参数名称:索引”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32216114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve the "Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
提问by Miss AA
Kindly help me fix the Error " Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index. Herewith is my code. Thank you
请帮我解决错误“索引超出范围。必须为非负且小于集合的大小。参数名称:索引。这是我的代码。谢谢
Dim index As Integer
Dim index1 As Integer
index1 = e.RowIndex + 1
//the error was here
index = GridView1.DataKeys(index1).Values(0)
index = Convert.ToInt32(index)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New MySqlConnection(constr)
Using cmd As New MySqlCommand("DELETE from tblauditplan WHERE AuditArea = @auditarea")
Using sda As New MySqlDataAdapter()
cmd.Parameters.AddWithValue("@auditplan", index)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Using
Me.BindGrid()
End Sub
回答by Shovers_
Dim index As Integer
Dim index1 As Integer
index1 = e.RowIndex + 1
//the error was here
index = GridView1.DataKeys(index1).Values(0)
index = Convert.ToInt32(index)
?? Why not just
?? 为什么不只是
index = GridView1.DataKeys(e.rowindex + 1).Values(0)
but yeah, you need to put in a check to make sure that you're not at the end of the rows.
但是,是的,您需要进行检查以确保您不在行的末尾。
if not index > e.rowCount
or something like that, without knowing what "e" is.
或类似的东西,不知道“e”是什么。

