My code does not remove any row from the database. These rows were shown in the listview in forms. They can be remove and disappear from the listview in the form but they are not removed from the database table.
Also, Im using MS Access for the database.
Here's the code
Public Sub DeleteProduct()
If ListView1.SelectedIndices.Count <= 0 Then
Return
End If
Dim ItemNo As Integer = ListView1.SelectedIndices(0)
Try
Dim I As Integer = MsgBox("Are you sure you want to delete this record? You can't Undo", MsgBoxStyle.YesNo, "Are you sure?")
If I = MsgBoxResult.Yes Then
conn.Open()
Dim cmd2 As New OleDb.OleDbCommand("DELETE FROM PRODUCT WHERE product_id = '" & ListView1.Items(ItemNo).SubItems(0).Text & "'", conn)
cmd2.ExecuteNonQuery()
MsgBox("Record removed successfully", MsgBoxStyle.OkOnly, "Remove Succeeded")
Else
Return
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
Refresh()
End Sub
.Text
property will break everything.MsgBox("Record removed successfully"
<-- This is wrong. You need to check the return-value ofExecuteNonQuery
first to verify that a row was deleted instead of simple assuming that it was successful.ListView1.Items(ItemNo).SubItems(0).Text
doesn't hold the value you expect, orproduct_id
is a number, not text.