This is a migrated thread and some comments may be shown as answers.

[Solved] Deleting records with RadGrid Control

1 Answer 43 Views
RadControls
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Terfa
Top achievements
Rank 1
Terfa asked on 12 Jan 2009, 08:29 AM

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!

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 13 Jan 2009, 10:57 AM
Hello Terfa,

Could you please confirm that you have added the "Username" field to the DataKeyNames collection of the grid MasterTableView?

Find more about deleting records with RadGrid here.

Sincerely yours,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
RadControls
Asked by
Terfa
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or