Cannot Update Datagrid

G

Guest

Hi. My application has dozens of datagrids but for some reason an exception
occurs when one of them is updated. When a user edits a datagrid row and
then clicks 'Update' the following exception occurs:
---------------------
"Exception Details: System.ArgumentOutOfRangeException: Specified argument
was out of the range of valid values. Parameter name: index"
---------------------
I've placed breakpoints with the UpdateCommand event, but the error occurs
before the breakpoints are reached. Below is more of my code. Could someone
please let me know why this error is occuring? Thanks.
---------------------
' Edit DataGrid
Private Sub DataGrid1_EditCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
Bind()
End Sub
---------------------
' Update DataGrid
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.UpdateCommand

' Retrieve the PK value
Dim ID As Integer = DataGrid1.DataKeys(e.Item.ItemIndex)

' Retrieve the Bundle (Textbox)
Dim Bundle As String
' Watch out the indices!
Bundle = (CType(e.Item.Cells(1).Controls(0), TextBox).Text)

' UPDATE command:
Dim updateCommand As New SqlCommand("UPDATE Bundle = @Bundle WHERE (ID =
@ID)", sqlConn)

' UPDATE command Parameters:
updateCommand.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID
updateCommand.Parameters.Add("@Bundle", SqlDbType.VarChar).Value = BundleID

updateCommand.Connection.Open()
updateCommand.ExecuteNonQuery()
DataGrid1.EditItemIndex = -1

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top