如何在asp.net c# web表单应用程序中将隐藏字段中的值转换为整数
本文关键字:字段 隐藏 整数 转换 应用程序 asp net 表单 web | 更新日期: 2023-09-27 18:06:09
我有一个名为hdn_ven_id的隐藏字段,当网格中的一行被选中时,它被填充为主键值(整数)。
protected void rg_vendors_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridDataItem item in rg_vendors.SelectedItems)
{
hdn_ven_id.Value = item["venId"].Text;
}
}
然后我使用hdn_ven_id的值在SqlCommand中填充@venIdFk。
protected void newDocUpldBtn_Click(object sender, EventArgs e)
{ ...
new_doc_cmd.Parameters.AddWithValue("@venIdFk", hdn_ven_id.Value);
...}
我得到以下错误:
将nvarchar值"81"转换为数据类型int时,转换失败。
81是正确的主键值,因此我知道隐藏字段正在捕获正确的值。
我已经回顾了几个线程,比如这个(将隐藏字段转换为整数)如何将隐藏字段的值从nvarchar转换为整数。然而,我不能得到隐藏字段的值进入数据库。如果我加上
int venidfk = int. parse (hdn_ven_id.Value);
到newDocUpldBtn_Click我得到错误"输入字符串格式不正确"。
如何将隐藏字段值转换为整数?
下面是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))
{
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.";
}
}
}
}
看起来您的隐藏字段包含一个逗号。要么找出它来自哪里并删除它,要么使用
new_doc_cmd.Parameters.AddWithValue("@venIdFk", hdn_ven_id.Value.Replace(",",""));