I have a listbox that is populated by a DataSet. I want to be able to delete a record when a item is selected and the user clicks the "Delete" button. But I am not able to figure it out. Here is what I have now for the code.
VB.NET:
Public Class frmSysco
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim dt As New DataSet
Dim db As New OleDb.OleDbDataAdapter
Dim bs As New BindingSource
Dim bsCat As New BindingSource
Dim daCat As New OleDb.OleDbDataAdapter
Dim dsCat As New DataSet
Dim bsPUnit As New BindingSource
Dim daPUnit As New OleDb.OleDbDataAdapter
Dim dsPUnit As New DataSet
Dim bsUvUM As New BindingSource
Dim daUvUM As New OleDb.OleDbDataAdapter
Dim dsUvUM As New DataSet
Dim sql As String
Private Sub frmSysco_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = D:\Ben's Documents\Work Things\Cost Manangment System\MainDatabase.accdb"
con.Open()
sql = "SELECT * FROM SyscoMasterPriceGuide"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "SyscoPrice")
con.Close()
bs.DataSource = ds.Tables("SyscoPrice")
bs.Sort = "ProductName"
lstName.DataSource = bs
lstName.DisplayMember = "ProductName"
lstName.ValueMember = "ID"
con.Open()
sql = "SELECT * FROM PurchaseCategories"
daCat = New OleDb.OleDbDataAdapter(sql, con)
daCat.Fill(dsCat, "Category")
con.Close()
bsCat.DataSource = dsCat.Tables("Category")
bsCat.Sort = "PurchaseCategory"
cboCategory.DataSource = bsCat
cboCategory.DisplayMember = "PurchaseCategory"
cboCategory.ValueMember = "ID"
con.Open()
sql = "SELECT * FROM PurchaseUnits"
daPUnit = New OleDb.OleDbDataAdapter(sql, con)
daPUnit.Fill(dsPUnit, "Unit")
con.Close()
bsPUnit.DataSource = dsPUnit.Tables("Unit")
bsPUnit.Sort = "Unit"
cboUM.DataSource = bsPUnit
cboUM.DisplayMember = "Unit"
cboUM.ValueMember = "ID"
con.Open()
sql = "SELECT * FROM UniversalUnit"
daUvUM = New OleDb.OleDbDataAdapter(sql, con)
daUvUM.Fill(dsUvUM, "Unit")
con.Close()
bsUvUM.DataSource = dsUvUM.Tables("Unit")
bsUvUM.Sort = "Unit"
cboUnivUM.DataSource = bsUvUM
cboUnivUM.DisplayMember = "Unit"
cboUnivUM.ValueMember = "ID"
MaxRows = ds.Tables("SyscoPrice").Rows.Count
Populate()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Dim dr As DataRow = DirectCast(bs.Current, DataRowView).Row
'I am missing something here
dr.Delete()
da.Update(ds, "SyscoPrice")
End Sub