Delete Item from Listbox connected to Access DB (>Code)
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'**** Connection to Acces DB
Dim MyConnection As OleDbConnection
MyConnection = New OleDbConnection()
MyConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\DataManipulatingTest\dbVoetbal.mdb;Persist Security Info=False"
MyConnection.Open()
'**** Filling DataSet
Dim DSPloegen As New DataSet()
Dim commPloeg As New OleDbDataAdapter()
Dim selectCMD As OleDbCommand = New OleDbCommand("SELECT * FROM Ploegen WHERE Klasse = '1'", MyConnection)
commPloeg.SelectCommand = selectCMD
commPloeg.Fill(DSPloegen, "Ploegen")
'**** Deleting Row from DataSet and Refreshing listbox
Dim drRij As DataRow = DSPloegen.Tables("Ploegen").Rows(lstPloegNamen.SelectedIndex)
DSPloegen.Tables("Ploegen").Rows.Remove(drRij)
DSPloegen.AcceptChanges()
lstPloegNamen.Items.Clear()
lstPloegNamen.DataSource = DSPloegen.Tables("Ploegen")
lstPloegNamen.DataTextField = "Ploegnaam"
lstPloegNamen.DataBind()
MyConnection.Close()
End Sub
This is my code behind the DeleteButton.
Items are listed in a listbox.
With this code I can only delete 1 item.
Why?
Something wrong with my code?
Please help!
Re: Delete Item from Listbox connected to Access DB (>Code)
Quote:
Originally posted by Kajiro
'**** Deleting Row from DataSet and Refreshing listbox
Dim drRij As DataRow = DSPloegen.Tables("Ploegen").Rows(lstPloegNamen.SelectedIndex)
DSPloegen.Tables("Ploegen").Rows.Remove(drRij)
DSPloegen.AcceptChanges()
With this code I can only delete 1 item.
Well.....Your only trying to delete one item from the list from what I can see. And I'm surprised your able to delete even one item.
The AcceptChanges() method of the DataSet is just re-setting the values to their original state. You have to remove that and place in a DataAdapter.UpDate() call to actually modify the DB.
John