2011-02-13 4 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"(try catch 내부)은 정수이고 사용자는 정수를 입력해야합니다. 사용자가 ABN에 대한 문자열을 입력하면 데이터베이스에 레코드 삽입을 중지하고 "다음을 입력하십시오. ABN ".이 코드에서 사용자가 메시지를 표시하는 문자열을 입력하고 ABN 값이 0 인 경우에도 여전히 레코드가 데이터베이스에 삽입됩니다. 어떻게 해결할 수 있습니까?GridView 행 업데이트

답변

0

첫째,이 방법으로 try catch를 사용하면 안됩니다.
정규식을 사용할 수있는 텍스트 상자의 유효성을 검사하려고합니다. 나는 그들에게 너무 익숙하지 않지만 다음 링크가 도움이 될 것입니다. No Validation 자세한 내용은 msdn source입니다.
귀하의 문제를 올바르게 이해하면 귀하의 문제를 해결할 것으로 생각합니다.