By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

Cannot Update Datagrid

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You haven't specified a table name in your update statement.

"UPDATE TABLENAME Set Bundle = @Bundle WHERE (ID = @ID)",

B Comrie
http://www.codewidgets.com
"MrMike" <Mr****@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.