How to convert value in a hiddenfield to an integer in an asp.net c# web forms application?
<p>I have a hiddenfield titled hdn_ven_id that is populated with the primary key value (integer) when a row in a grid is selected.</p>

<pre><code>protected void rg_vendors_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridDataItem item in rg_vendors.SelectedItems)
    {
        hdn_ven_id.Value = item["venId"].Text;                
    }
}
</code></pre>

<p>Then I use the value from hdn_ven_id to populate @venIdFk in a SqlCommand.</p>

<pre><code>protected void newDocUpldBtn_Click(object sender, EventArgs e)
{ ...
    new_doc_cmd.Parameters.AddWithValue("@venIdFk", hdn_ven_id.Value);
...}
</code></pre>

<p>I get the following error:</p>

<blockquote>
  <p>Conversion failed when converting the nvarchar value '81,' to data type int.</p>
</blockquote>

<p>81 is the correct primary key value, so I know that the correct value is being captured by the hidden field.</p>

<p>I have reviewed several threads like this one (<a href="https://stackoverflow.com/questions/12720665/can-not-convert-hiddenfield-to-int32">Convert HiddenField to Integer</a>) on how to convert the value of the hiddenfield from an nvarchar to an integer.  However,  I can't get the value of the hidden field to go into the database.  If I add </p>

<blockquote>
  <p>int venidfk = int.Parse(hdn_ven_id.Value);</p>
</blockquote>

<p>to newDocUpldBtn_Click I get the error "Input string was not in a correct format". </p>

<p>How can I convert the hiddenfield value to an integer?</p>

<p>Here is all of newDocUpldBtn_Click:</p>

<pre><code>protected void newDocUpldBtn_Click(object sender, EventArgs e)
{
    int venidfk = int.Parse(hdn_ven_id.Value);

    //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, docPath, docStat, 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(@docPath)), LTRIM(RTRIM(@docStat)), 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("@docPath", hdn_doc_path.Value);
            new_doc_cmd.Parameters.AddWithValue("@docStat", "4");
            new_doc_cmd.Parameters.AddWithValue("@venIdFk", venidfk);

            drap_cnxn.Open();
            new_doc_cmd.ExecuteNonQuery();
            drap_cnxn.Close();

            if (IsPostBack)
            {
                ven_doc_det_upld_status_lbl.Text = "Your document was successfully uploaded.";

                docTitleTextBox.Text = "";
                docTypeDdl.Text = "";
                docContractTypeDdl.SelectedValue = "";
                docOrgDdl.Text = "";
                docDeptDdl.Text = "";
                docDescTextBox.Text = "";
                priorContCdTextBox.Text = "";
                legCompContIdTextBox.Text = "";
                hdn_doc_path.Value = "";

                rg_vendors.DataBind();
                fv_ven_docs.DataBind();

            }
            else
            {
                ven_doc_upld_fail_lbl.Text = "Your document failed to upload.  Please contact Compliance for assistance.";
            }
        }
    }
}
</code></pre>