title: insert into ms access db from another database with c# code
body: <p>I use the following code to insert a record from one database to another but it doesn't work. I tried the query in <code>MS-ACCESS 2007</code> and it works fine but it doesn't work when called programmatically from my <code>C#</code> code?</p>
  
  <pre><code>string query_insert = "INSERT INTO Questionnaires_Table(BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees) "
      + "SELECT BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees "
      + "FROM Questionnaires_Table IN '" + dialog.FileName + "' Where Branch_ID = " + textBox1.Text ;
  
  dbConnDest.Open();
  
  
     OleDbDataAdapter dAdapter = new OleDbDataAdapter();
     OleDbCommand cmd_insert = new OleDbCommand(query_insert, dbConnDest);
  
     dAdapter.InsertCommand = cmd_insert;
     cmd_insert.ExecuteNonQuery();
  
  dbConnDest.Close();
  </code></pre>
  
  <p>When I take the the content of <code>query_insert</code> in ms access, it works fine</p>
  
  <p>It throws </p>
  
  <blockquote>
    <p>INSERT INTO syntax error exception in line 
    cmd_insert.ExecuteNonQuery();</p>
  </blockquote>
  
  <p>EDIT</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Data.OleDb;
  
  namespace WindowsFormsApplication4
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
  
              OpenFileDialog dialog = new OpenFileDialog();
  
              dialog.Title = "select database";
  
  
  
              if ((dialog.ShowDialog() == DialogResult.OK) &amp;&amp; (textBox1.Text == ""))
              {
                      MessageBox.Show("insert reference year", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
              else
              {
  
  
  
  
                      OleDbConnection dbConnDest;
                      dbConnDest = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\SystemA.accdb;Persist Security Info=False;");
  
                      try
                      {
  
  
  
                          string query_insert = "INSERT INTO Questionnaires_Table(BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees) "
                                                 + "SELECT BranchName,Factor,Region,Branch_ID,Current_Date,No_Employees "
                                                 + "FROM Questionnaires_Table1 IN '" + dialog.FileName + "' Where ReferenceYear = " + textBox1.Text + ";";
  
  
  
  
                          dbConnDest.Open();
  
  
  
                          OleDbCommand cmd_insert = new OleDbCommand(query_insert, dbConnDest);
  
                          try
                          {
                              cmd_insert.ExecuteNonQuery();
                          }
                          catch (Exception g)
                          {
                              MessageBox.Show(g.ToString());
                          }
  
  
  
                          textBox2.Text = query_insert.ToString();
  
                          dbConnDest.Close();
                      }
                      catch (Exception h)
                      {
                          MessageBox.Show(h.ToString());
                      }
  
  
  
                  }
  
  
          }
  
      }
  }
  </code></pre>
  
  <p>EDIT</p>