Cannot Update Datagrid

Discussion in 'ASP .Net' started by =?Utf-8?B?TXJNaWtl?=, Dec 8, 2004.

  1. 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
     
    =?Utf-8?B?TXJNaWtl?=, Dec 8, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?TXJNaWtl?=

    B. Comrie Guest

    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" <> wrote in message
    news:...
    > 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
     
    B. Comrie, Dec 8, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matthew schouppe
    Replies:
    3
    Views:
    2,365
    Scott Allen
    Apr 30, 2004
  2. =?Utf-8?B?TWljaGFlbA==?=

    How to update datagrid after update

    =?Utf-8?B?TWljaGFlbA==?=, Mar 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    504
    Alex D.
    Mar 17, 2006
  3. =?Utf-8?B?bWlrZWc=?=

    ASP1 DataGrid Cannot Update /Read entered cell value.

    =?Utf-8?B?bWlrZWc=?=, Jun 26, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    398
    =?Utf-8?B?bWlrZWc=?=
    Jun 26, 2007
  4. narke
    Replies:
    1
    Views:
    702
  5. Andreas Klemt
    Replies:
    0
    Views:
    313
    Andreas Klemt
    Oct 8, 2003
Loading...

Share This Page