【发布时间】:2011-02-13 06:24:53
【问题描述】:
if (((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text == "Insert")
{
VendorProperties VP = new VendorProperties();
VP.VendorName = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text.ToString();
try
{
VP.ABN = Convert.ToInt32(((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text.ToString());
}
catch
{
lblMessage.Text = "Please Enter a Number for ABN";
}
VP.VendorAddress1 = ((TextBox)GridView1.Rows[0].Cells[5].Controls[0]).Text.ToString();
VP.VendorAddress2 = ((TextBox)GridView1.Rows[0].Cells[6].Controls[0]).Text.ToString();
VP.VendorAddress3 = ((TextBox)GridView1.Rows[0].Cells[7].Controls[0]).Text.ToString();
VP.State = ((TextBox)GridView1.Rows[0].Cells[8].Controls[0]).Text.ToString();
VP.PostCode= ((TextBox)GridView1.Rows[0].Cells[9].Controls[0]).Text.ToString();
VP.ContractorVendorContactName = ((TextBox)GridView1.Rows[0].Cells[10].Controls[0]).Text.ToString();
VP.Phone = ((TextBox)GridView1.Rows[0].Cells[11].Controls[0]).Text.ToString();
DropDownList DR = (DropDownList)GridView1.Rows[0].Cells[12].FindControl("DropDownList1");
VP.EmailAddressID= Convert.ToInt32(DR.SelectedValue.ToString());
PinDicDAO.InsertVendor(VP);
}
以上是我通过 gridview 行更新事件为数据库插入记录的代码。项目“VP.ABN”(在try catch中)是一个整数,用户应该输入一个整数。如果用户输入ABN的字符串,我需要停止将记录插入数据库并打印消息“请输入一个数字ABN”。在此代码中,即使用户输入了一个字符串,它也会显示消息,并且仍然将记录插入到数据库中,因为 ABN 值为 0。我该如何解决这个问题?
【问题讨论】:
标签: asp.net validation gridview try-catch