Insert into table selected from combobox

Discussion in 'ASP.NET / ASP.NET Core' started by josef, Oct 12, 2011.

  1. i have a form with two comboboxes and two texboxes .I have multiple tables with different names but same column name such as column1 and column2. I want to insert text box1 value into column1 in the table selected from combobox1 and textbox2 into column2 in table selected from combobox2 such as if i select a table1 from combobox1 and table2 from combobox2 and click on the button, data should be inserted into both table1 and table2 .when i run the code it , i dont get any error but it does not load the tables in comboboxes . can someone please examine the code
    Thanks in advance



    Code:
    Imports System.Data.OleDb
    
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub
        Private conn As OleDbConnection
        Private sda As OleDbDataAdapter
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\dtb.accdb;Jet OLEDB:Database Password=****")
            sda = New OleDbDataAdapter([String].Empty, conn)
            conn.Open()
    
            ComboBox2.DataSource = conn.GetSchema("TABLES")
            ComboBox2.ValueMember = "TABLE_NAME"
            ComboBox2.DisplayMember = "TABLE_NAME"
    
            ComboBox1.DataSource = conn.GetSchema("TABLES")
            ComboBox1.ValueMember = "TABLE_NAME"
            ComboBox1.DisplayMember = "TABLE_NAME"
        End Sub
    
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            If ComboBox1.SelectedItem IsNot Nothing AndAlso ComboBox2.SelectedItem IsNot Nothing AndAlso TextBox1.Text IsNot Nothing Then
                Dim cmd As OleDbCommand = conn.CreateCommand()
                cmd.CommandText = "INSERT INTO " & ComboBox1.SelectedItem & " (col1) VALUES ('" & TextBox1.Text & "')"
    
                 sda.InsertCommand = cmd
                sda.InsertCommand.ExecuteNonQuery()
                cmd.CommandText = "INSERT INTO " & ComboBox2.SelectedItem & " (col2) VALUES ('" & TextBox2.Text & "')"
    
                sda.InsertCommand = cmd
                sda.InsertCommand.ExecuteNonQuery()
                sda.Dispose()
                conn.Close()
                conn.Dispose()
            End If
        End Sub
    End Class
    
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    I didn't have time to review the entire code, but one thing that sticks out is the path (Data Source=C:\dtb.accdb). It looks like you're using an Access database, so please use the server path provided to you in the Control Panel.
     

Share This Page