2011-02-13 86 views
0
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”(里面尝试捕获)是一个整数,用户应该输入一个整数。如果用户输入一个字符串为ABN我需要停止记录插入到数据库,只是打印消息“请输入一个数字为ABN“。在这段代码中,即使用户输入了一个字符串,它也会显示消息,并且仍将记录插入到数据库中,因为ABN值为0.我该如何解决这个问题?GridView行更新

回答

0

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