Data Validation

Hi, I am a beginner in vb.NET. I would like to know that how to check the validation of data entered by the user when they leave the textbox.

Besides that, I would like to know how am i going to display the number of records inside a database in a textbox.



  • Hi,
    The word validation means are looking to format the textbox by certain rules...

    some samples.

    datme = Format(TextBox24.Text, "dd/MM/yyyy HH:mm ")
    LTrim(textbox1.Text).Substring(0, 1)
    Textbox -casing - upper

    Can be useful for verifying the strings.

    Are you looking for this? Is it enough?

  • Thanks for your reply.

    Did I make any mistake in the terms I used ? If there is pls correct me.

    Actually my question is how to check whether the data enter into the textbox match the field or not ,and an error message will pop up the moment the user move away from the textbox, if the data is invalid for the particular field. For example, if the field is product ID, how to ensure that the user only enter number and not alphabets.

    Another question is I have a database containing two tables - Product Info and product supplier. I have a window form now and there is a textbox in it which will display the number of records that have been saved into the database. How to code the textbox so that it will show the correct number of records even after we add or delete records from the database.

    Thanks for your help.
  • Suppose you are having datatable contains the data.[dtb4]

    Dim DTB3 As New DataTable
    Dim query3 As String = "select * from emp_master order by
    Dim DS2 As New DataSet

    con.ConnectionString = ConStr
    Dim DA3 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(query3, con)
    DTB3 = DS2.Tables(0)

    For 2nd Question


    For 1st Question

    From your Textbox-keydown

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    kychr = Space(1)
    Select Case e.KeyValue
    'Case Is < 48
    ' TextBox2.AppendText("Another key was pressed!" & vbCrLf & vbCrLf)
    Case 48 To 57
    'TextBox2.AppendText("Key is in 0 to 9 range." & vbCrLf & vbCrLf)
    kychr = ChrW(e.KeyCode)
    Case 65 To 70
    'TextBox2.AppendText("Key is 'A', 'B', 'C', 'D', 'E', or 'F'" & vbCrLf & vbCrLf)
    kychr = ChrW(e.KeyCode)
    'Case Is > 70
    ' TextBox2.AppendText("Another key was pressed!" & vbCrLf & vbCrLf)
    End Select
    Select Case e.KeyCode

    Case Keys.NumPad0
    kychr = "0"
    Case Keys.NumPad1
    kychr = "1"
    Case Keys.NumPad2
    kychr = "2"
    Case Keys.NumPad3
    kychr = "3"
    Case Keys.NumPad4
    kychr = "4"
    Case Keys.NumPad5
    kychr = "5"
    Case Keys.NumPad6
    kychr = "6"
    Case Keys.NumPad7
    kychr = "7"
    Case Keys.NumPad8
    kychr = "8"
    Case Keys.NumPad9
    kychr = "9"

    End Select
    kyprs = RTrim(kyprs + kychr)

    If e.KeyCode = Keys.Back Or e.KeyCode = Keys.Delete Then
    If (kyprs.Length) > 0 Then
    kyprs = kyprs.Substring(0, kyprs.Length - 1)
    End If

    End If
    If e.KeyCode = Keys.Enter Then
    call data_check()


    private sub data_check()
    dim m107 As Integer,dbnd1 As New BindingSource
    dbnd1.DataSource = DTB3
    m107 = dbnd1.Find("emp_id", thatha)

    Do While m107<0

    msgbox("Invalid Text")

    end sub


    Hope This May Helps You

  • seancampbellseancampbell Pennsylvania, USA
    Hi xSoulx, You are correct in thinking that the term "Validation" refers to checking (or validating) data that is entered to ensure it is in a correct format to be put into the database (the letters ABC cannot go into an integer field, so validation is quite important when working with databases).

    I prefer to validate data when the user tries to Add or Update the record (so when they click the Save button is when the validation takes place). To do this I usually write a big If statement that checks each text entry field, and prompts the user if it is incorrect.

    Check out this post from last month:

    I was helping someone else with Validation and wrote an example of a subroutine that checks if Textboxes are blank. You can just edit it to verify that the data is in String, Integer, or Double format by using the correct logic (If IsNumeric(TextBox1.Text) = False Then 'Not a number) etc....

    Now your question about having a count of records from the database... If you connect to the database and get a count of the records in it once, you can store that number in a Global variable (in other words, a variable that is declared outside of a subroutine, but inside the form, so it will retain it's value from routine to routine) and increment or decrement it each time you add or delete respectively... You could also just do a SQL command to count each time too, but that involves an extra call to the database. Both methods have their draw backs.

    Please review this, and respond with any additional questions or if you'd like me to better describe something...

  • Thanks for your reply, MAHEY and seancampbell.

    I need further explanation regarding the validation and record count since i am new to vb.NET. Is there any recommended tutorial on vb.NET syntax and the way to program in order for me to understand further ? Please suggest some for me.

    Actually i have the logic but it's just that i do not know how to put the logic into code.

    Anyway, thanks again.
  • Thanks for your links.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!