Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a databound combobox using VB2010 and MS access as the DB.. When i click on a button, the selected item in the combobox will be deleted from the database and no longer display in the combobox...
Posted

Public Class Form1

    Const _ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\adam_syirah\Documents\Database1.accdb;"
    Private dtProducts As New System.Data.DataTable("Products")

        Private Sub PopulateCombo()
            Using conn As New System.Data.OleDb.OleDbConnection(_ConnectionString)
                conn.Open()
            Dim sqlCmd As New System.Data.OleDb.OleDbCommand("select * from Products", conn)
                Dim dr As System.Data.OleDb.OleDbDataReader = sqlCmd.ExecuteReader()
                dtProducts.Load(dr)
            End Using

            ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", dtProducts, "ProductId", True, DataSourceUpdateMode.OnPropertyChanged))
            ComboBox1.DisplayMember = "Name"
            ComboBox1.DataSource = dtProducts
        End Sub

        Private Sub ComboDeleteExample_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            PopulateCombo()
        End Sub

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If ComboBox1.SelectedItem IsNot Nothing AndAlso TypeOf (ComboBox1.SelectedItem) Is System.Data.DataRowView Then
                Using conn As New System.Data.OleDb.OleDbConnection(_ConnectionString)
                    conn.Open()
                    Dim row As System.Data.DataRowView = DirectCast(ComboBox1.SelectedItem, System.Data.DataRowView)
                Dim sqlcmd As New System.Data.OleDb.OleDbCommand(String.Format("delete * from Products where productid={0}", row.Item("ProductID")), conn)
                    If sqlcmd.ExecuteNonQuery() = 1 Then
                        row.Delete()
                        dtProducts.AcceptChanges()
                    End If
                End Using
            End If
        End Sub
    End Class
 
Share this answer
 
v2
Comments
Christian Graus 4-Feb-12 21:21pm    
Please edit your post, don't post your own code as an 'answer'
Rajesh Anuhya 4-Feb-12 22:02pm    
Edited: Code Tags added
--RA
Rajesh Anuhya 4-Feb-12 22:03pm    
You are not supposed to post here,
--RA
This sounds completely trivial. Your drop list should have a data value that tells you the primary key of the object, then you delete it. Where is the issue ? What is your code ?
 
Share this answer
 
Comments
Member 8600170 4-Feb-12 20:12pm    
i'm a newbie in vb.net.. i don't have any code.. can u give an example please.. TQ..
Christian Graus 4-Feb-12 21:22pm    
Looking at the code you posted, I assume you're trying to teach yourself, because it's certainly not good enough for anyone to use. You should choose more simple tasks at first and learn one thing at a time, instead of getting confused and asking us for code, which is not the same as learning

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