GridView行更新

时间:2011-02-13 06:24:54

标签: asp.net validation gridview try-catch

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时,仍会将记录插入数据库中。如何解决此问题?

1 个答案:

答案 0 :(得分:0)

首先,你不应该以这种方式使用try catch 您想要验证文本框,您可以使用正则表达式。我不太熟悉它们,但以下链接应该有所帮助。 No Validation如果你需要更多的帮助,这是msdn source 如果我能正确理解你的问题,我认为这可以解决你的问题。

相关问题