Click here to Skip to main content
15,885,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that is connected to my database. What I'm trying to do is when I click the delete button the selected item in the combo box should delete the entire row in my database and it should reload the combo box with values from the database.
Below is my code for the DELETE BUTTON it actually deletes the data but it doesn't update the value of my combo box instead it duplicates the value in my Combobox every time I click delete.

What I have tried:

Using cons As New OleDb.OleDbConnection(conSTR)

        cons.Open()
        Dim cmd As New OleDb.OleDbCommand("DELETE FROM Judges WHERE = @FullName")
        cmd.Parameters.AddWithValue("@FullName", cmbJudges.Text)
        MessageBox.Show("DATA HAS BEEN DELETED")
    End Using

    cmbJudges.Text = ""
    txtFullNameJudge.Text = Nothing
    txtContactJudge.Text = Nothing
    txtUserNameJudge.Text = Nothing
    txtPasswordJudge.Text = Nothing

    Using cons As OleDb.OleDbConnection = New OleDb.OleDbConnection(conSTR)

        Try

            With cmd
                .Connection = cons
                .Connection.Open()
                .CommandText = "SELECT FullName from Judges"
                rdr = cmd.ExecuteReader

                While rdr.Read
                    cmbJudges.Items.Add(rdr.Item(0))
                End While
                rdr.Close()
                .Connection.Close()
            End With

        Catch ex As Exception

            Dim test = MsgBox(ex.Message)

        End Try

    End Using
Posted
Updated 11-Mar-20 21:45pm

1 solution

You need to clear combobox list before you start adding new values: ComboBox.ObjectCollection.Clear Method (System.Windows.Forms) | Microsoft Docs[^]
Or
You need to prevent adding duplicate values: Add and Remove Items from ComboBox, ListBox, or CheckedListBox Control - Windows Forms | Microsoft Docs[^]

Alternativelly, you can re-bind combobox's datasource:

VB.NET
Dim judge = cmbJudges.Text
Dim affected As Integer = 0
Dim dt As DataTable = New DataTable()
Dim comm As String() = {"DELETE FROM Judges WHERE FullName = @FullName", "SELECT FullName FROM Judges;"}
Using cons As OleDb.OleDbConnection = New OleDb.OleDbConnection(conSTR)
    'delete judge
	Using cmd As New OleDb.OleDbCommand(comm(0), cons)
        cons.Open()
		cmd.Parameters.AddWithValue("@FullName", judge)
		affected = cmd.ExecuteNonQuery()
		cons.Close()
	End Using
	'how many records has been affected?	
	If affected >0 Then
		'get list of judges
		Using cmd As New OleDb.OleDbCommand(comm(1), cons)
	        cons.Open()
	        Using rdr As OleDb.OleDbDataReader = cmd.ExecuteReader()
	        	dt.Load(rdr)
			End Using
	        cons.Close()
	    End Using
	End If
End Using

With cmbJudges
	.DataMember = "FullName"
	.ValueMember = "FullName"
	.DataSource = dt
End With
 
Share this answer
 
v3
Comments
Maciej Los 12-Mar-20 6:32am    
I do not see in your code usage of Clear method.
lelouch_vi 2 12-Mar-20 6:41am    
Using cons As New OleDb.OleDbConnection(conSTR)

cons.Open()
Dim cmd As New OleDb.OleDbCommand("DELETE FROM Judges WHERE = @FullName")
cmd.Parameters.AddWithValue("@FullName", cmbJudges.Text)
MessageBox.Show("DATA HAS BEEN DELETED")
End Using

cmbJudges.Text = ""
txtFullNameJudge.Text = Nothing
txtContactJudge.Text = Nothing
txtUserNameJudge.Text = Nothing
txtPasswordJudge.Text = Nothing
cmbJudges.Items.Clear()

Using cons As OleDb.OleDbConnection = New OleDb.OleDbConnection(conSTR)

Try

With cmd
.Connection = cons
.Connection.Open()
.CommandText = "SELECT FullName from Judges"
rdr = cmd.ExecuteReader

While rdr.Read
cmbJudges.Items.Add(rdr.Item(0))
End While
rdr.Close()
.Connection.Close()
End With

Catch ex As Exception

Dim test = MsgBox(ex.Message)

End Try

End Using
Maciej Los 12-Mar-20 7:10am    
See updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900