i create a code to delete a data in sql server..but nothing works anyone outhere help me spot the problem..

]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim varAdapter As SqlDataAdapter
        MsgBox("are you sure want to delete?, important information will be erase. ")
        'varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1") 'STEP 1
        'varConnection.Open() 'STEP 2
        varQuery = "DELETE FROM tbl_rop WHERE Name_User ='" & TextBox1.Text & "', Date_of_birth= '" & TextBox2.Text & "' , Hobbies='" & TextBox3.Text & "' , Phone_Number ='" & TextBox4.Text & "'" 'STEP 3
        varCommand = New SqlCommand(varQuery, varConnection)

        varCommand.ExecuteNonQuery()

        varCommand.Dispose() 'STEP 6
        varConnection.Close() 'STEP 7
        varAdapter.Update(varDataSet)
    End Sub

Recommended Answers

All 2 Replies

There are 2 more steps you need to add.
1. Create an Update Command to Update the Record with an Update statement (like the Delete statement)

2. Add the 2 Commands (varCommand and the new Update Command) to the Adapter.
varAdapter.DeleteCommand = varCommand
varAdapter.UpdateCommand = varUpdateCommand

Thats all

i put the code as u said but nothing happen are u sure this will work?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim varAdapter As SqlDataAdapter
        
        MsgBox("are you sure want to delete?, important information will be erase. ")
        varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1") 'STEP 1
        varConnection.Open() 'STEP 2
        varQuery = "DELETE FROM tbl_rop WHERE Name_User ='" & TextBox1.Text & "', Date_of_birth= '" & TextBox2.Text & "' , Hobbies='" & TextBox3.Text & "' , Phone_Number ='" & TextBox4.Text & "'" 'STEP 3
        varCommand = New SqlCommand(varQuery, varConnection)
        varAdapter.DeleteCommand = varCommand
        varAdapter.UpdateCommand = varUpdateCommand

        varCommand.ExecuteNonQuery()

        varCommand.Dispose() 'STEP 6
        varConnection.Close() 'STEP 7
        varAdapter.Update(varDataSet)
    End Sub
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.