title: INSERT INTO table with foreign key contraints
body: <p>I have read a couple things on this and came up with the following code. I am using an Access database and am coding in C# through Visual Studio. </p>
  
  <p>I am getting a syntax error in this. I have tried to create a query in Access to test this but its difficult to create this in that.</p>
  
  <p>Can anyone help me figure out why this isnt working?</p>
  
  <pre><code>using(OleDbConnection conn1 = new OleDbConnection(global::InsulationProjectTracker.Properties.Settings.Default.InsulationDB))
  {
      using(OleDbCommand command1 = conn1.CreateCommand())
      {
          conn.Open();
  
          command.CommandText = "INSERT INTO Jobsites (CustomerID, JobsiteName) VALUES ((SELECT Customers.CustomerID FROM Customers WHERE Customers.CustomerName = @cname1 AND Customers.BranchNumber = @bNumber1), @jName1)";
          command.Parameters.Add(new OleDbParameter("@cName1", cboCustomerName.Text));
          command.Parameters.Add(new OleDbParameter("@bNumber1", cboBranch.Text));
          command.Parameters.Add(new OleDbParameter("@jName1", txtJobsiteName.Text));
  
          command.ExecuteNonQuery();
  
          conn.Close();
          command.Parameters.Clear();
  
      }
  }
  </code></pre>
  
  <p>Tables setup</p>
  
  <h2>Branches</h2>
  
  <p>PK BranchNumber<br>
  BranchName  </p>
  
  <h2>Customers</h2>
  
  <p>PK CustomerID<br>
  CustomerName<br>
  FK BranchNumber  </p>
  
  <h2>Jobsites</h2>
  
  <p>PK JobsiteID<br>
  JobsiteName<br>
  FK CustomerID  </p>
  
  <p>EDIT***</p>
  
  <p>the below commmand produces no error, but also doesnt insert data into database</p>
  
  <pre><code>command.CommandText = "INSERT INTO Jobsites (CustomerID, JobsiteName) SELECT @jName1, c.CustomerID FROM Customers c WHERE c.CustomerName = @cname1 AND c.BranchNumber = @bNumber1";
  </code></pre>