title: How to obtain the primary key of the record being inserted?
body: <p>How do I obtain the primary key of the record being inserted, and use it as the foreign key in another Insert Into statement?  Can I use a hidden field to store the value?  If so, how do I set the value of the hidden field?</p>
  
  <p>This is an ASP.NET Web Forms c# application.</p>
  
  <p>I tested the document upload by itself and it works as expcted.  But now I need to add the second Insert Into statement for the docStats table.  </p>
  
  <p>Here is the code behind now:</p>
  
  <pre><code>protected void newDocUpldBtn_Click(object sender, EventArgs e)
      {
          //Save the file to the server and set the full path
          int i = 0;
          FileUpload fu = docFU;
          string filename = fu.FileName;
          string fnnnoext = System.IO.Path.GetFileNameWithoutExtension(fu.FileName);
          string fnnextonly = System.IO.Path.GetExtension(fu.FileName);
  
          if (fu.HasFile)
          {
              while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
              {
                  i++;
                  filename = (fnnnoext + "(" + i.ToString() + ")" + fnnextonly);
              }
              fu.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
  
              hdn_doc_path.Value = (Server.MapPath("~/Data/") + filename);
              hdn_doc_path_no_ext.Value = (fnnnoext + "(" + i.ToString() + ")" + fnnextonly);
  
              SqlConnection drap_cnxn = new SqlConnection("Data Source=WDBSVCPRD01\\SVCDB;Initial Catalog=drap;Integrated Security=True");
              {
                  SqlCommand new_doc_cmd = new SqlCommand("Insert Into docs(docTitle, docType, docContractType, docOrg, docDept, docDesc, PriorContCd, LegCompContId, docUpldDt, docUpldBy, docPath, venIdFk) Values(LTRIM(RTRIM(@docTitle)), LTRIM(RTRIM(@docType)), LTRIM(RTRIM(@docContractType)), LTRIM(RTRIM(@docOrg)), LTRIM(RTRIM(@docDept)), LTRIM(RTRIM(@docDesc)), LTRIM(RTRIM(@PriorContCd)), LTRIM(RTRIM(@LegCompContId)), LTRIM(RTRIM(@docUpldDt)), LTRIM(RTRIM(@docUpldBy)), LTRIM(RTRIM(@docPath)), LTRIM(RTRIM(@venIdFk)))", drap_cnxn);
                  new_doc_cmd.Parameters.AddWithValue("@docTitle", docTitleTextBox.Text);
                  new_doc_cmd.Parameters.AddWithValue("@docType", docTypeDdl.SelectedValue);
                  new_doc_cmd.Parameters.AddWithValue("@docContractType", docContractTypeDdl.SelectedValue);
                  new_doc_cmd.Parameters.AddWithValue("@docOrg", docOrgDdl.SelectedValue);
                  new_doc_cmd.Parameters.AddWithValue("@docDept", docDeptDdl.SelectedValue);
                  new_doc_cmd.Parameters.AddWithValue("@docDesc", docDescTextBox.Text);
                  new_doc_cmd.Parameters.AddWithValue("@PriorContCd", priorContCdTextBox.Text);
                  new_doc_cmd.Parameters.AddWithValue("@LegCompContId", legCompContIdTextBox.Text);
                  new_doc_cmd.Parameters.AddWithValue("@docUpldDt", DateTime.Now.ToString());
                  new_doc_cmd.Parameters.AddWithValue("@docUpldBy", System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                  new_doc_cmd.Parameters.AddWithValue("@docPath", hdn_doc_path.Value);
                  new_doc_cmd.Parameters.AddWithValue("@venIdFk", hdn_ven_id.Value);
  
                  drap_cnxn.Open();
                  new_doc_cmd.ExecuteNonQuery();
                  //GET THE VALUE OF docIdPk FROM THE INSERTED RECORD
                  drap_cnxn.Close();
  
                  if (IsPostBack)
                  {
                      //USE THE PRIMARY KEY FROM THE DOCUMENT INSERTED ABOVE FOR docIdFk IN THE FOLLOWING ...
  
                      SqlCommand new_doc_stat_cmd = new SqlCommand("Insert Into docStats(docStat, docStatDt, docStatSetBy, docIdFk) Values(LTRIM(RTRIM(@docStat)), LTRIM(RTRIM(@docStatDt)), LTRIM(RTRIM(@docStatSetBy)), LTRIM(RTRIM(@docIdFk)))", drap_cnxn);
                      new_doc_stat_cmd.Parameters.AddWithValue("@docStat", "1");
                      new_doc_stat_cmd.Parameters.AddWithValue("@docStatDt", DateTime.Now.ToString());
                      new_doc_stat_cmd.Parameters.AddWithValue("@docStatSetBy", System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                      new_doc_stat_cmd.Parameters.AddWithValue("@docIdFk", ??????);
  
                      drap_cnxn.Open();
                      new_doc_stat_cmd.ExecuteNonQuery();
                      drap_cnxn.Close();                          
                  }
              }
          } 
      }
  </code></pre>