ive been develop a project known as ordering system
i need to delete data from db by clicking the button
ive follow a tutorial from utube and to the exact same code he been used
error
"object reference not set to an instance of an object"
ive double check my db dan the code in vb, there nothing wrong
i hope some could help me
code used for db :
Private Sub Delete(id As String)
Dim str As String = "server = localhost; userid = root; password = 1234 ; database = kedaikopimamba_db"
Dim MysqlConnection As New MySqlConnection(str)
MysqlConnection.Open()
Dim sql As String
Dim cmdDeleteDrink As MySqlCommand
sql = "DELETE FROM foodtable,drinktable,desserttable WHERE ID = '" + id + "'"
cmdDeleteDrink = New MySqlCommand(sql, MysqlConnection)
'open connection and insert
Try
adapterDelete.DeleteCommand = MysqlConnection.CreateCommand()
adapterDelete.DeleteCommand.CommandText = sql
'prompt confirmation
If MessageBox.Show("Are sure to delete this item ?", "DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.OK Then
If cmdDeleteDrink.ExecuteNonQuery() > 0 Then
MessageBox.Show("Item Successful deleted. ")
End If
End If
MysqlConnection.Close()
Catch ex As Exception
MysqlConnection.Clone()
MessageBox.Show(ex.Message)
End Try
End Sub
the button code :
Private Sub btnDeleteItem_Click(sender As Object, e As EventArgs) Handles btnDeleteItem.Click
Dim id As String = dgDisplayItem.SelectedRows(0).Cells(0).Value
Delete(id)
End Sub