2015-08-26 53 views
-2

請幫助我解決錯誤「索引超出範圍。非負數且小於集合的大小參數名:。指標。由此是我的代碼謝謝如何解決「錯誤:索引超出範圍,必須是非負數,小於集合的大小參數名稱:索引」

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 
+1

使用調試器,並找出它。 –

+1

你的代碼沒有檢查'index1 = e.RowIndex + 1'之前是否有足夠的行' – Plutonix

+0

謝謝你的評論Plutonix和T McKeown。我會嘗試你的建議/評論。 –

回答

0
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) 

?? 爲什麼不

index = GridView1.DataKeys(e.rowindex + 1).Values(0) 

,但沒錯,你需要加入一個chec k以確保你不在行的末尾。

if not index > e.rowCount 

或類似的東西,不知道「e」是什麼。

相關問題