trying to delete selectedItem from combobox to remove selectected account from database using mysql and vb.net 2008....this is my code for my delete menu can anyone help?
***************************************************

Public Class delete

    Public Sub _load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ProbillernewDataSet.customer' table. You can move, or remove it, as needed.
        Me.CustomerTA1.Fill(Me.ProbillernewDataSet.customer)
       
    End Sub
    Private Sub bntCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCancle.Click
        Me.Close()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'delete selected row from database
        
        Dim Ans As MsgBoxResult
        ' Set icon to exclamation mark
        ' Set buttons to OK and Cancel
        ' Show Message Box
        Ans = MsgBox("Are you sure you want to Delete Selected Account?", vbYesNo + vbExclamation, "Yes or No")
        ' If the user clicked no exit sub
        If Ans = vbNo Then
            Exit Sub
        End If
        'if the user clicked yes then delete selected row from database
        If Ans = vbYes Then
            Dim account_number As String
            account_number = CBAccountNumber.SelectedItem(0)

            'Dim i As String
            'CBAccountNumber.Text = CBAccountNumber.SelectedItem(0)
            'i = CBAccountNumber.SelectedItem(0)

            Dim mysql As String
            mysql = "SELECT * DELETE FROM customer WHERE account_number="""
            ' selects dependent record from Contacts, deletes then refreshes

        End If

        Me.Close()
    End Sub
End Class
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
 If CBAccountNumber.SelectedIndex = -1 Then
            Return 'nothing selected
        End If

        Dim result As MsgBoxResult = MsgBox("Are you sure you want to Delete Selected Account?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Yes or No")

        If result = MsgBoxResult.No Then
            Return
        End If



        Dim account_number As String = CBAccountNumber.SelectedItem.ToString

        If Not IsNumeric(account_number) Then
            MsgBox("wrong format") 'selected item is not a number
            Return
        End If

        Dim query As String = "DELETE FROM customer WHERE account_number=" & account_number
         'assuming you have mysql connection function already...
        Dim cmd As New SqlCommand(query, myConnection)
        cmd.ExecuteNonQuery()
        myConnection.Close()
End Sub

Thanks but it didn't exactly work...I'm deleteing an account from MySQLdatabase (already loaded) using combobox to select the account number, then refresh the datagrid on main page...


Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
 If CBAccountNumber.SelectedIndex = -1 Then
            Return 'nothing selected
        End If

        Dim result As MsgBoxResult = MsgBox("Are you sure you want to Delete Selected Account?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Yes or No")

        If result = MsgBoxResult.No Then
            Return
        End If



        Dim account_number As String = CBAccountNumber.SelectedItem.ToString

        If Not IsNumeric(account_number) Then
            MsgBox("wrong format") 'selected item is not a number
            Return
        End If

        Dim query As String = "DELETE FROM customer WHERE account_number=" & account_number
         'assuming you have mysql connection function already...
        Dim cmd As New SqlCommand(query, myConnection)
        cmd.ExecuteNonQuery()
        myConnection.Close()
End Sub

Try Dim query As String = String.Format("DELETE FROM customer WHERE account_number='{0}'", account_number)

Thanks but it didn't exactly work...I'm deleteing an account from MySQLdatabase (already loaded) using combobox to select the account number, then refresh the datagrid on main page...

Thanks I tried that but for some reson it still won't work

then try putting in a breakpoint and check that the account_number contains the correct value when you try to use it

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.