title: "cannot insert null value to primary key" error while trying to insert a non-null value
body: <p>i have a composite key(<strong>col1, col2</strong>) in my table<br/>
  i get the "cannot insert null value to primary key" error on col1 but actually im not trying to insert null value, <strong>i've checked in debug mode, the value is not null</strong><br/>
  any ideas?
  <br/><br/>
  this is my stored procedure:<br/></p>
  
  <pre><code>CREATE PROCEDURE [dbo].[proc1]
  @col1 char(11) = NULL,
  @col2 nchar(50) = NULL,
  @col3 real = NULL
  AS
  BEGIN
  SET NOCOUNT ON;
  
  -- Insert statements for procedure here
  update dbo.table1 set col2 = @col2 where col1 = @col1 AND col3 = @col3
  if @@ROWCOUNT=0
  insert into dbo.table1 ( col1, col2, col3 ) values ( @col1, @col2, @col3 )
  </code></pre>
  
  <p><br/>
  <br/>
  this is my code:<br/></p>
  
  <pre><code>SqlConnection conn = create_conn();
  conn.Open();
  SqlCommand command = new SqlCommand("another_proc", conn);
  command.Parameters.Add("col1", SqlDbType.Char).Value = col1;
  SqlDataAdapter da = new SqlDataAdapter(command);
  DataSet ds = new DataSet();
  da.Fill(ds);
  
  command = new SqlCommand("proc1", conn);
  
  // here i set the value of the parameter
  **command.Parameters.Add("col1", SqlDbType.Char).Value = col1;**
  
  command.Parameters.Add("col2", SqlDbType.NChar);
  command.Parameters.Add("col3", SqlDbType.Real);
  double y, yk, ym, yy;
  int k, m;
  string col2;
  foreach (DataRow dr in ds.Tables[0].Rows)
  {
      col2 = dr["col2"].ToString().Trim();
      yk = Double.Parse(dr["yk"].ToString());
      ym = Double.Parse(dr["ym"].ToString());
      yy = Double.Parse(dr["yy"].ToString());
      k = Int32.Parse(dr["k"].ToString());
      m = Int32.Parse(dr["m"].ToString());
      y = Double.Parse(dr["y"].ToString());
      double col3 = ...some calculation here...;
      command.Parameters["col2"].Value = col2;
      command.Parameters["col3"].Value = col3;
      command.ExecuteNonQuery();
  }
  conn.Close();
  </code></pre>
  
  <p><br/><br/>
  updated code:<br/></p>
  
  <pre><code>SqlConnection conn = create_conn();
  conn.Open();
  SqlCommand command = new SqlCommand("another_proc", conn);
  command.Parameters.Add("col1", SqlDbType.Char).Value = col1;
  SqlDataAdapter da = new SqlDataAdapter(command);
  DataSet ds = new DataSet();
  da.Fill(ds);
  
  command.CommandText = "proc1";
  command.Parameters.Add("col2", SqlDbType.NChar);
  command.Parameters.Add("col3", SqlDbType.Real);
  double y, yk, ym, yy;
  int k, m;
  string col2;
  foreach (DataRow dr in ds.Tables[0].Rows)
  {
      col2 = dr["col2"].ToString().Trim();
      yk = Double.Parse(dr["yk"].ToString());
      ym = Double.Parse(dr["ym"].ToString());
      yy = Double.Parse(dr["yy"].ToString());
      k = Int32.Parse(dr["k"].ToString());
      m = Int32.Parse(dr["m"].ToString());
      y = Double.Parse(dr["y"].ToString());
      double col3 = k * (yk / 100) + m * (ym / 100) + y * (yy / 100);
      command.Parameters["col2"].Value = col2;
      command.Parameters["col3"].Value = col3;
      command.ExecuteNonQuery();
  }
  conn.Close();
  </code></pre>