Insert into two tables selected from two combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • josef gonjales
    New Member
    • Dec 2010
    • 2

    Insert into two tables selected from two combobox

    I am using the code below to display selected table in datagrid but
    when i add one more comobobox and a datagridview and use same code , the second does not write into the table.
    i want to insert into tables selected from both combobox such as if i select a table1 from combobox1 and
    tble2 from combobox2 and click on the button, data should be inserted into both table1 and table2.
    i would like to know (if it possible) how to get it to work.Thanks in advance

    Code:
    Private con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\deletedb.accdb;Persist Security Info=False")
        Private adapter As New OleDbDataAdapter(String.Empty, Me.con)
        Private data As DataTable
    
        Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles MyBase.Load
            con.Open()
    
            Me.ComboBox1.DisplayMember = "TABLE_NAME"
            Me.ComboBox1.ValueMember = "TABLE_NAME"
            Me.ComboBox1.DataSource = Me.con.GetSchema("TABLES")
            con.Close()
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
                                               ByVal e As EventArgs) Handles ComboBox2.SelectedIndexChanged
            If Me.ComboBox1.SelectedItem IsNot Nothing Then
                Me.data = New DataTable
                Me.adapter.SelectCommand.CommandText = String.Format("SELECT * FROM [{0}]", Me.ComboBox1.SelectedValue)
                Me.adapter.Fill(data)
    
                Me.DataGridView1.DataSource = Nothing
                Me.DataGridView1.Columns.Clear()
                Me.DataGridView1.DataSource = Me.data
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As Object, _
                               ByVal e As EventArgs) Handles Button1.Click
            If Me.data IsNot Nothing Then
                Dim builder As New OleDbCommandBuilder(Me.adapter)
                Me.adapter.Update(Me.data)
            End If
        End Sub
Working...