Update control button

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Rudy (Rudy_at_discussions.microsoft.com)
Date: 03/04/05


Date: Fri, 4 Mar 2005 11:55:03 -0800

Hello All!
Working on a data grid, with the edit button. Having problems with the
update button. I know my connection is being made, but I have tried so many
diffrent things, and I keep getting diffrent errors. I'm hoping sombody can
look at my code and tell what I'm doing wrong.

Public Sub grdProducts_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grdProducts.UpdateCommand

        Dim Sqlconnection1 As New SqlConnection(SQL_CONNECTION_STRING)
        Dim cmdProd As SqlCommand
        Dim txtProductName As String = e.Item.Cells(1).Text
        Dim txtSupplier As String = e.Item.Cells(2).Text
        Dim txtCost As String = e.Item.Cells(4).Text
        Dim txtSRP As String = e.Item.Cells(5).Text
        Dim txtInStock As String = e.Item.Cells(3).Text
        Dim strUpdate As String

        Dim scmd As New SqlCommand(strUpdate, Sqlconnection1)

                With scmd.Parameters
      
           
              .Add(New SqlParameter("@ProductID", _
               SqlDbType.Int)).Value = _
               CInt(grdProducts.DataKeys(grdProducts.SelectedIndex).ToString
= txbProductID.Text)
            grdProducts.EditItemIndex = 1
          

            .Add(New SqlParameter("@ProductName", _
              SqlDbType.NVarChar, 40)).Value = e.Item.Cells(2)

            .Add(New SqlParameter("@SupplierPart", _
           SqlDbType.NVarChar, 50)).Value = e.Item.Cells(3)

            .Add(New SqlParameter("@UnitCost", _
                SqlDbType.Money, 8)).Value = e.Item.Cells(4)

            .Add(New SqlParameter("@UnitSRP", _
                 SqlDbType.Money, 8)).Value = e.Item.Cells(5)

            .Add(New SqlParameter("@UnitsInStock", _
                   SqlDbType.Int)).Value = e.Item.Cells(6)

            .Add(New SqlParameter("@Discontinued", _
                SqlDbType.Bit)).Value = e.Item.Cells(5)

            '' '.Add(New SqlParameter("@New", _
            '' ' SqlDbType.Bit)).Value = chkNew.Checked
            .Add(New SqlParameter("@Used", _
              SqlDbType.Bit)).Value = e.Item.Cells(5)

        End With
        Sqlconnection1.Open()

        scmd.ExecuteNonQuery()
        BindProductsGrid()
         Sqlconnection1.Close()
           End Sub

Sorry if my code is a little messy, I been trying diffrent things, and
commenting alot of stuff. I took all the comments out.

The error i get now is "Index was out of range. Must be non-negative and
less than the size of the collection. Parameter name: index "

I know I'm pretty close, I'm just not sure where I'm going wrong.

TIA!!!

Rudy


Quantcast