user1916528 user1916528 - 1 year ago 154
C# Question

How to convert value in a hiddenfield to an integer in an c# web forms application?

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.

protected void rg_vendors_SelectedIndexChanged(object sender, EventArgs e)
foreach (GridDataItem item in rg_vendors.SelectedItems)
hdn_ven_id.Value = item["venId"].Text;

Then I use the value from hdn_ven_id to populate @venIdFk in a SqlCommand.

protected void newDocUpldBtn_Click(object sender, EventArgs e)
{ ...
new_doc_cmd.Parameters.AddWithValue("@venIdFk", hdn_ven_id.Value);

I get the following error:

Conversion failed when converting the nvarchar value '81,' to data type int.

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

I have reviewed several threads like this one (Convert HiddenField to Integer) 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

int venidfk = int.Parse(hdn_ven_id.Value);

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

How can I convert the hiddenfield value to an integer?

Here is all of newDocUpldBtn_Click:

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))
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);


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 = "";


ven_doc_upld_fail_lbl.Text = "Your document failed to upload. Please contact Compliance for assistance.";

Answer Source

It looks like your hidden field includes a comma. Either figure out where that's coming from and remove it, or use

 new_doc_cmd.Parameters.AddWithValue("@venIdFk", hdn_ven_id.Value.Replace(",",""));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download