0

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
3
Contributors
6
Replies
27
Views
2 Months
Discussion Span
Last Post by Shark_1
0

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.

2

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

Votes + Comments
Should be correct
0

@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.

Edited by Shark_1

0

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.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.