title: How to insert data in a table with foreign key
body: <p>I want to insert data in table <code>Article</code> which has a foreign key to table <code>Categorie</code>.</p>
  
  <p>I created a form with TextBoxes and I put the foreign key in a bounded ComboBox to <code>Categorie</code> table. This is the code I use:</p>
  
  <pre><code>private void button15_Click(object sender, EventArgs e)
  {
      SqlConnection con3 = new SqlConnection();
      con3.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\GestStock.mdf;Integrated Security=True;User Instance=True";
      con3.Open();
  
      SqlCommand insertCommande1 = new SqlCommand("INSERT INTO [Article] (CodeArticle, LibArticle, IdCategorie, Quantite, StockActuel, StockMinimum, PrixAchat, DateAchat, NumeroFacture)" +
                "VALUES(@CodeArticle, @LibArticle, @IdCategorie, @Quantite, @StockActuel, @StockMinimum, @PrixAchat, @DateAchat, @NumeroFacture)", con3);
  
      insertCommande1.Parameters.AddWithValue("@CodeArticle", codeArticleTextBox1.Text);
      insertCommande1.Parameters.AddWithValue("@LibArticle", libArticleTextBox.Text);
      insertCommande1.Parameters.AddWithValue("@IdCategorie",idCategorieComboBox.Text);
  
      insertCommande1.Parameters.AddWithValue("@Quantite", Convert.ToInt32(quantiteTextBox1.Text));
      insertCommande1.Parameters.AddWithValue("@StockActuel", Convert.ToInt32(stockActuelTextBox1.Text));
      insertCommande1.Parameters.AddWithValue("@StockMinimum", Convert.ToInt32(stockMinimumTextBox1.Text));
      insertCommande1.Parameters.AddWithValue("@PrixAchat", Convert.ToInt32(prixAchatTextBox1.Text));
      insertCommande1.Parameters.AddWithValue("@DateAchat", Convert.ToDateTime(dateAchatDateTimePicker1.Text));
      insertCommande1.Parameters.AddWithValue("@NumeroFacture", numeroFactureTextBox.Text);
  
      insertCommande1.ExecuteNonQuery();
  
      MessageBox.Show("L'article est ajouté avec succées");
      con3.Close();
  }
  </code></pre>
  
  <p>But when I execute this code the <code>Article</code> is added successfully but when I exit the application and re-run it I get this error message:</p>
  
  <blockquote>
    <p>Failed to enable constraints. One or more rows contain values that violate type constraints not null, unique, or foreign key.</p>
  </blockquote>
  
  <p>I know that it is caused by this </p>
  
  <pre><code>insertCommande1.Parameters.AddWithValue("@IdCategorie", idCategorieComboBox.Text);
  </code></pre>
  
  <p>So my question is how to do insert query in a correct way??</p>
  
  <p>Thanks in advance.</p>