locked
issues deleting a row from a gridview RRS feed

  • Question

  • User945405123 posted

    hi again. i have some problems with deleteing a row in a gridview. i cant get it right. honestly i cant understand it below are the code. thank you in advance

        Sub gvPostings_DeleteRow(ByVal sender As Object, ByVal E As GridViewCommandEventArgs)
            Dim sqlStr As String = "DELETE FROM tblSales WHERE fdlSales_ID = @fdlSales_ID;"
            Dim myConnection As New SqlConnection(myConStr)
            Dim mySqlCommand As SqlCommand = New SqlCommand(sqlStr, myConnection)
            mySqlCommand.Parameters.Add(New SqlParameter("@fdlSales_ID", SqlDbType.Int, 11))
            mySqlCommand.Parameters("@fdlSales_ID").Value = CInt(gvPostings.DataKeyNames("fdlSales_ID"))
            mySqlCommand.Connection.Open()
            Try
                mySqlCommand.ExecuteNonQuery()
    
            Catch ex As Exception
    
            End Try
            mySqlCommand.Connection.Close()
    
            gvPostings.DataBind()
    
        End Sub

    here is the htm code

        <asp:GridView ID = "gvPostings" runat = "server" BorderWidth = "0" CellPadding = "2" Width = "600" HeaderStyle-HorizontalAlign = "Center" HeaderStyle-Font-Bold = "true" AlternatingRowStyle-BackColor = "Beige" AutoGenerateColumns = "false" OnRowDeleting = "gvPostings_DeleteRow" AutoGenerateSelectButton = "true" AutoGenerateDeleteButton = "True" DataKeyNames = "fdlSales_ID">
                             <Columns>
                                <asp:BoundField ReadOnly = "true" HeaderText = "No." DataField = "fdlSales_ID" ItemStyle-Width = "50" />
                                <asp:BoundField ReadOnly = "true" HeaderText = "Created By" DataField = "fldUser_ID" ItemStyle-Width = "50" />
                                <asp:BoundField ReadOnly = "true" HeaderText = "Title" DataField = "fldTitle" ItemStyle-Width = "400"/>
                                <asp:BoundField ReadOnly = "true" HeaderText = "Category" DataField = "fldCategory_Name" ItemStyle-Width = "150"/>
                                <asp:BoundField ReadOnly = "true" HeaderText = "Status" DataField = "fldStatus" ItemStyle-Width = "150"/>
                              </Columns> 
                          </asp:GridView > 
    Wednesday, October 26, 2011 9:50 AM

Answers

  • User3866881 posted

    mySqlCommand.Parameters.Add(New SqlParameter("@fdlSales_ID", SqlDbType.Int, 11))

    You don't need to assign "11", but just do this:

     mySqlCommand.Parameters.Add("@fdlSales_ID", SqlDbType.Int)

     mySqlCommand.Parameters[0].Value = CInt(gvPostings.DataKeyNames("fdlSales_ID"))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 27, 2011 10:27 PM

All replies

  • User465171450 posted

    Are you displaying the exception ex anywhere? I ask because if there is a problem, the way this is written the exception is caught but nothing is done with it so you wouldn't know if you have one. You could write the exception to the trace log or remove the try/catch just to see if an error is being thrown that you aren't aware of.

    Wednesday, October 26, 2011 10:19 AM
  • User-1208031168 posted

    Please post the error you receive. It could be of many reasons. The code given looks fine.

    Wednesday, October 26, 2011 11:43 AM
  • User3866881 posted

    mySqlCommand.Parameters.Add(New SqlParameter("@fdlSales_ID", SqlDbType.Int, 11))

    You don't need to assign "11", but just do this:

     mySqlCommand.Parameters.Add("@fdlSales_ID", SqlDbType.Int)

     mySqlCommand.Parameters[0].Value = CInt(gvPostings.DataKeyNames("fdlSales_ID"))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 27, 2011 10:27 PM