Hi guys. Im trying to delete a row from a table in DataGridView (VB 2010) and my database (MS Access 2013) but on "Delete" it shows that it doesnt get the ID of that data row, so it wouldnt actually know what data to "Delete"

delete btn:

Private Sub btn_dlt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_dlt.Click
        Dim delete_confirmation = MsgBox("Are you sure you would like to delete the product '" & currentpid & "'?", MsgBoxStyle.YesNo)

        If delete_confirmation = MsgBoxResult.Yes Then

            run_sql_query("DELETE FROM TBL_PRODUCTS_A154287 WHERE FLD_PRODUCT_ID = '" & currentpid & "'")

            MsgBox("The product '" & currentpid & "' has been successfully deleted.")

        End If
End Sub

initializing currentpid as public

 Dim currentpid As String

currentpid :

  Private Sub getProductID()
        Dim current_row As Integer = grd_list.CurrentRow.Index
        currentpid = grd_list(0, current_row).Value

        txt_id.Text = currentpid 
        txt_pname.Text = grd_list(1, current_row).Value
    End Sub

    Private Sub grd_list_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grd_list.CellContentClick
    End Sub

Your SQL statement is quite right.

There is no mistake to get pid in the textbox. But where the codes in the sub procedure run_sql_query() ? Is there any mistake ? God knows.

run_sql_query() runs correctly as i used it to get the data into the datagrid.

i just realized it might be because of how i set up the form. user has to select a category from the combobox which is populated by refresh_grid() only then the table will be filled with the data. is this why "delete" does not work?


Private Sub refresh_grid(ByVal category As String)
        grd_list.DataSource = run_sql_query("SELECT FLD_PRODUCT_ID,FLD_PRODUCT_NAME,FLD_PRICE,FLD_BRAND,FLD_DESC,FLD_QTY FROM TBL_PRODUCTS_A154287 WHERE FLD_TYPE = '" & category & "'")

        grd_list.Columns(0).HeaderText = "Product ID"
        grd_list.Columns(1).HeaderText = "Product Name"
        grd_list.Columns(2).HeaderText = "Price"
        grd_list.Columns(3).HeaderText = "Brand"
        grd_list.Columns(4).HeaderText = "Description"
        grd_list.Columns(5).HeaderText = "Quantity"
    End Sub

combobox selected:

    Private Sub cmb_type_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_type.SelectedIndexChanged
    End Sub

fill the table:

 Private Sub frm_productlist_a154287_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmb_type.DataSource = run_sql_query("SELECT FLD_TYPE FROM TBL_PRODUCTS_A154287 GROUP BY FLD_TYPE")
        cmb_type.DisplayMember = "FLD_TYPE"

    End Sub

If the job ofthe function run_sql_query() is to populate dataadapter and datatable then it cannot delete a record from the table. So you have to create another function / sub procedure to delete a record.

right. right. gosh. created a new function. thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.