title: Insert into C# with SQLCommand
body: <p>What's the best way to INSERT data into a database?</p>
  
  <p>This is what I have but it's wrong..</p>
  
  <pre><code>cmd.CommandText = "INSERT INTO klant(klant_id,naam,voornaam) VALUES(@param1,@param2,@param3)";
  
  cmd.Parameters.Add(new SqlParameter("@param1", klantId));
  cmd.Parameters.Add(new SqlParameter("@param2", klantNaam));
  cmd.Parameters.Add(new SqlParameter("@param3", klantVoornaam));
  </code></pre>
  
  <p>The function add data into the listBox </p>
  
  <p><a href="http://www.pictourl.com/viewer/37e4edcf" rel="noreferrer">http://www.pictourl.com/viewer/37e4edcf</a> <em><code>(link is dead)</code></em></p>
  
  <p>but not into the database..</p>
  
  <p><a href="http://www.pictourl.com/viewer/4d5721fc" rel="noreferrer">http://www.pictourl.com/viewer/4d5721fc</a> <em><code>(link is dead)</code></em></p>
  
  <p>The full function:</p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
  {            
      conn2 = new SqlConnection();
      conn2.ConnectionString = ConfigurationManager.ConnectionStrings["connSpionshopString"].ConnectionString;
  }
  
  private void button2_Click(object sender, EventArgs e)
  {         
      string sqlCmd = "SELECT naam,voornaam,klant_id FROM klant;";
      SqlCommand cmd = new SqlCommand(sqlCmd, conn2);
  
      conn2.Open();
  
      using(SqlDataReader reader = cmd.ExecuteReader())
      {
          while (reader.Read())
          {
              listBox2.Items.Add(reader.GetString(0) + " " + reader.GetString(1) + "  (" + reader.GetInt16(2) + ")");
          }  
      }
      conn2.Close();
  }
  
  private void button4_Click(object sender, EventArgs e)
  {
      int klantId = Convert.ToInt32(textBox1.Text);
      string klantNaam = textBox2.Text;
      string klantVoornaam = textBox3.Text;
  
      conn2.Open();
  
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = conn2;
      cmd.CommandText = "INSERT INTO klant(klant_id, naam, voornaam)   VALUES(@param1,@param2,@param3)";
  
      cmd.Parameters.AddWithValue("@param1", klantId);
      cmd.Parameters.AddWithValue("@param2", klantNaam);
      cmd.Parameters.AddWithValue("@param3", klantVoornaam);
  
      cmd.ExecuteNonQuery(); 
  
      conn2.Close();
  }
  </code></pre>