0

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

Edited by __avd: Add [code] tags. Encase your code in: [code] and [/code]

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Geekitygeek
0
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
0

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
0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.