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

adapterDelete.DeleteCommand = MysqlConnection.CreateCommand()
adapterDelete.DeleteCommand.CommandText = sql

These two lines are caused for your error. adapterDelete does not declare in your code. except that every thing are ok.

That may be the case except that without seeing all the code we don't know if it was declared at a higher scope.

commented: Should be correct +8

@Jim: You are correct.
But adapterDelete has no use in the procedure and all objects like connectionstring, mysqlcommand are declared before use them except that one. IDE can poit out the fault in codes by the exception object reference not set to an instance of an objectat the time of debugging and bulding the solution if you do not declare the object in your project publicly/privately before use it.

This codes is copied by the OP from YouTube which he/she already declared and may be made some changes however he knows but has no conception how much he can copy it and which portion he has to delete/use.

Most of the DB applications I wrote only required one connection so I typically declared the connection object at the top level and used it globally, opening and closing it from within each module as required so I assumed that may be the case here. I actually have no idea what type of object adapterDelete is.

It should be a DataAdapter object. It has various type of property to maintain a database by using a command object.

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.