2011-11-29 58 views
1

我试过这样的事情,即使认为nric是错误的,它也会插入到数据库中。 因此,我希望它停止插入数据到数据库时,nric验证是错误的,但从我做什么,结果是它仍然插入名称....所以应该改变,以便它停止插入,直到用户更改值,然后可以继续插入?如何在文本框的验证错误时停止插入?

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click 
     register() 

    End Sub 

Protected Sub nricValidate() 
     Dim strRegex As String = "^([sS]\d{7}[a-zA-Z])$" 
     Dim myRegex As Regex = New Regex(strRegex) 
     Dim strNr As String = txtNRIC.Text 
     Dim nric As String = txtNRIC.Text 
     If String.IsNullOrEmpty(txtNRIC.Text) Then 

     ElseIf myRegex.IsMatch(strNr) Then 
      Dim nricArray() As Char = nric.ToArray 
      Dim sum As Integer = 0 
      Dim num As Integer = 0 
      Dim result As Integer = 0 
      Dim numbers As Char 
      Dim no As String = "" 

      Dim i As Integer = 0 
      Do While (i < nricArray.Length) 
       If (i = 1) Then 
        num = 0 
        numbers = nricArray(i) 
        no = numbers.ToString 
        num = Convert.ToInt32(no) 
        num = (num * 2) 
        nricArray(i) = Convert.ToChar(num) 
       ElseIf (i = 2) Then 
        num = 0 
        numbers = nricArray(i) 
        no = numbers.ToString 
        num = Convert.ToInt32(no) 
        num = (num * 7) 
        nricArray(i) = Convert.ToChar(num) 
       ElseIf (i = 3) Then 
        num = 0 
        numbers = nricArray(i) 
        no = numbers.ToString 
        num = Convert.ToInt32(no) 
        num = (num * 6) 
        nricArray(i) = Convert.ToChar(num) 
       ElseIf (i = 4) Then 
        num = 0 
        numbers = nricArray(i) 
        no = numbers.ToString 
        num = Convert.ToInt32(no) 
        num = (num * 5) 
        nricArray(i) = Convert.ToChar(num) 
       ElseIf (i = 5) Then 
        num = 0 
        numbers = nricArray(i) 
        no = numbers.ToString 
        num = Convert.ToInt32(no) 
        num = (num * 4) 
        nricArray(i) = Convert.ToChar(num) 
       ElseIf (i = 6) Then 
        num = 0 
        numbers = nricArray(i) 
        no = numbers.ToString 
        num = Convert.ToInt32(no) 
        num = (num * 3) 
        nricArray(i) = Convert.ToChar(num) 
       ElseIf (i = 7) Then 
        num = 0 
        numbers = nricArray(i) 
        no = numbers.ToString 
        num = Convert.ToInt32(no) 
        num = (num * 2) 
        nricArray(i) = Convert.ToChar(num) 
       End If 
       i = (i + 1) 
      Loop 
      i = 0 
      Do While (i < nricArray.Length) 
       If ((i > 0) _ 
          AndAlso (i < 8)) Then 
        numbers = nricArray(i) 
        num = Convert.ToInt32(numbers) 
        sum = (sum + num) 
       End If 
       i = (i + 1) 
      Loop 
      result = (sum Mod 11) 
      If (result = 10) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(65)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'A' Nric Error" 
       End If 
      ElseIf (result = 9) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(66)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'B' Nric Error" 
       End If 
      ElseIf (result = 8) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(67)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'C'Nric Error" 
       End If 
      ElseIf (result = 7) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(68)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'D'Nric Error" 
       End If 
      ElseIf (result = 6) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(69)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'E'Nric Error" 
       End If 
      ElseIf (result = 5) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(70)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'F'Nric Error" 

       End If 
      ElseIf (result = 4) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(71)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'G'Nric Error" 
       End If 
      ElseIf (result = 3) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(72)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'H'Nric Error" 
       End If 
      ElseIf (result = 2) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(73)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'I'Nric Error" 
       End If 
      ElseIf (result = 1) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(90)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'Z'Nric Error" 
       End If 
      ElseIf (result = 0) Then 
       If (nricArray(8) = Microsoft.VisualBasic.ChrW(74)) Then 

       Else 

        txtNRIC.Focus() 
        ResultLabel.Text = "last alphabet should be 'J'Nric Error" 
       End If 
      End If 



      Return 
     Else 
      ResultLabel.Text = "The NRIC is incorrect!" 
      txtNRIC.Text = String.Empty 
      txtNRIC.Focus() 
     End If 


    End Sub 

Protected Sub register() 

     Dim myConn As New SqlConnection 
     Dim myCmd As New SqlCommand 
     myConn.ConnectionString = ConfigurationManager.ConnectionStrings("Company").ConnectionString 
     Dim cmd As String 
     cmd = "Insert into Customer values (@fullName, @nric) " 
     myCmd.CommandText = cmd 
     myCmd.CommandType = CommandType.Text 

     nricValidate() 

     myCmd.Parameters.Add(New SqlParameter("@fullName", txtName.Text)) 

     myCmd.Parameters.Add(New SqlParameter("@nric", txtNRIC.Text)) 


     myCmd.Connection = myConn 
     myConn.Open() 
     myCmd.ExecuteNonQuery() 
     myCmd.Dispose() 
     myConn.Dispose() 




    End Sub 

回答

3

这大循环是完全不必要的。有很多事情要解决,我会做一个大的改写。我把216行改为41,没问题。它最有可能做得更好。

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click 
    If nricValidate() Then 
     Using myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("Company").ConnectionString), 
       myCmd As SqlCommand = myConn.CreateCommand() 
      myCmd.CommandText = "INSERT INTO Customer VALUES(@fullName, @nric)" 
      myCmd.CommandType = CommandType.Text 

      myCmd.Parameters.Add(New SqlParameter("@fullName", txtName.Text)) 
      myCmd.Parameters.Add(New SqlParameter("@nric", txtNRIC.Text)) 

      myConn.Open() 
      myCmd.ExecuteNonQuery() 
     End Using 
    End If 
End Sub 

Protected Function nricValidate() As Boolean 
    Dim myRegex As New Regex("^([sS]\d{7}[a-zA-Z])$") 

    If Not String.IsNullOrEmpty(txtNRIC.Text) AndAlso myRegex.IsMatch(txtNRIC.Text) Then 
     Dim nricArray(txtNRIC.Text.Length - 1) As Integer 
     Dim sum As Integer = 0 

     For i As Integer = 1 To 7 
      sum += Integer.Parse(txtNRIC.Text.Substring(i, 1)) * If(i = 1, 2, 9 - i) 
     Next 

     If nricArray(8) <> 75 - sum Mod 11 Then 
      txtNRIC.Focus() 
      ResultLabel.Text = "The last value should be " & (75 - sum Mod 11).ToString() & ": NRIC Error" 
      Return False 
     End If 

     Return True 
    Else 
     ResultLabel.Text = "The NRIC is incorrect!" 
     txtNRIC.Text = String.Empty 
     txtNRIC.Focus() 
    End If 

    Return False 
End Function 

您的实际答案是 - 你需要把nricValidateFunction,返回一个成功的值,并插入到数据库之前检查成功。但是你可以看到,其余的代码也可以进行大量优化。

我确实提前道歉,但这是我一生中见过的最差的代码。请详细阅读如何编程。

+0

对于我作为整数= 0〜nric.Length - 1这行代码,在nric上有错误,在哪里声明nric? – devilking

+0

@devilking:对不起,现在修好了。这是一个剩余的。 – Ryan

+0

我有一个错误sayin输入字符串不正确的格式? BT如果我拿走验证我插入工作执行,所以有功能nricValidate问题? – devilking

1

改变你的nricValidate返回true,如果验证通过

Protected Function nricValidate() As Boolean 
    'Return True if validation pass 
End Function 

,那么你可以验证并继续

If nricValidate() Then 
    Dim myConn As New SqlConnection 
    Dim myCmd As New SqlCommand 
    myConn.ConnectionString = ConfigurationManager.ConnectionStrings("Company").ConnectionString 
    Dim cmd As String 
    cmd = "Insert into Customer values (@fullName, @nric) " 
    myCmd.CommandText = cmd 
    myCmd.CommandType = CommandType.Text 
    myCmd.Parameters.Add(New SqlParameter("@fullName", txtName.Text)) 
    myCmd.Parameters.Add(New SqlParameter("@nric", txtNRIC.Text)) 
    myCmd.Connection = myConn 
    myConn.Open() 
    myCmd.ExecuteNonQuery() 
    myCmd.Dispose() 
    myConn.Dispose() 
End If 

另一种方式是,如果ResultLabel对验证文本失败检查执行前数据库操作。

1

您必须在每次验证失败时制作一个return false。像这样:

If (nricArray(8) = Microsoft.VisualBasic.ChrW(65)) Then 

    Else 

     txtNRIC.Focus() 
     ResultLabel.Text = "last alphabet should be 'A' Nric Error" 
     return False 
    End If 
... 
    return True 'at the end of the function 

不要忘记给分更改为布尔返回类型的函数。

Protected function nricValidate() as Boolean 

,然后里面您注册子

取代nricValidate()if not nricValidate() then exit sub并使其bfore任何声明,所以不需要设置什么...

0

由于您发布错误的结果标签,你可以使用这个对象按钮内提交检测错误:

If (String.IsNullOrEmpty(ResultLabel.Text)) Then 
    ' valid,continue 
End IF