Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6860817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:27:22+00:00 2026-05-27T02:27:22+00:00

i tried something like this, it insert into the database even thought nric is

  • 0

i tried something like this, it insert into the database even thought nric is wrong.
So i want it to stop inserting the data into the database when the nric validation is wrong, however from what i do, the result is it still insert the name in….so where should change to allow it stop inserting until user change the value then can continue insert???

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:27:22+00:00Added an answer on May 27, 2026 at 2:27 am

    That big loop is entirely unnecessary. There’s so much to fix here that I’ll do a big rewrite. I turned 216 lines into 41, no problem. It could most likely be made much better, too.

    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
    

    Your actual answer is – you need to turn nricValidate into a Function, return a success value, and check for success before inserting into the database. But the rest of your code could be heavily optimized too, as you can see.

    I do apologize in advance, but that is the worst code I’ve ever seen in my entire life. Please read up on how to program in general.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
I tried something like this but it just makes the background of the image
How can I match (PCRE) everything inbetween two tags? I tried something like this:
Recently I tried compiling program something like this with GCC: int f(int i){ if(i<0){
i have never tried something like this so i need your help. i have
I have a xml file which contains data I want to insert into a
Is there something like die in JavaScript? I've tried with break, but doesn't work
Looking for something like: $(input:radio:checked).previous(name, original_name).attr(name,new_name); I tried a few different seen around here
is it possible to create something like a global dictionary for pspell? I tried
Lasty, I tried to implements an hybrid structure in Java, something that looks like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.