I'm having an exception with the following code, which is intended to delete records from a table
here is the code:
Protected Sub RadGrid1_DeleteCommand1(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.DeleteCommand
'Get the GridDataItem of the RadGrid
Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
'Get the primary key value using the DataKeyValue.
Dim GridUserName As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("Username").ToString
Try
'Open the SqlConnection
SqlConnection.Open()
Dim deleteQuery As String = "DELETE FROM Logindata WHERE Username ='" & GridUserName & "'"
SqlCommand.CommandText = deleteQuery
SqlCommand.Connection = SqlConnection
SqlCommand.ExecuteNonQuery()
'Close the SqlConnection
SqlConnection.Close()
Catch ex As Exception
RadGrid1.Controls.Add(New LiteralControl("Unable to delete Administrator. Reason: " + ex.Message))
e.Canceled = True
End Try
End Sub
The error messaged displayed was:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
I'd really appreciate every assistance to help fix the exception, thank you!