Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Validating textbox value against a db

Validating textbox value against a db

Scheduled Pinned Locked Moved Visual Basic
databasesql-serversysadmintutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spettiford
    wrote on last edited by
    #1

    I have a textbox where a user enters his/her id number. After a click event, I want to take that id number and compare it to the id that is stored on my sql server database table, then allow the user to either proceed or stop. Any ideas how to do that? Shannon P.

    C 1 Reply Last reply
    0
    • S spettiford

      I have a textbox where a user enters his/her id number. After a click event, I want to take that id number and compare it to the id that is stored on my sql server database table, then allow the user to either proceed or stop. Any ideas how to do that? Shannon P.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Use SQL to check if the value is in the table. Use the result of the SQL to decide what to do next. More specific help would require an idea of your overall setup, and which part of this task is causing you problems. Christian Graus - Microsoft MVP - C++

      S 1 Reply Last reply
      0
      • C Christian Graus

        Use SQL to check if the value is in the table. Use the result of the SQL to decide what to do next. More specific help would require an idea of your overall setup, and which part of this task is causing you problems. Christian Graus - Microsoft MVP - C++

        S Offline
        S Offline
        spettiford
        wrote on last edited by
        #3

        What I'm attempting to do is to validate what a user's id is within a textbox with what is stored within the sql server database. If the id on the textbox matches what is inside the database, begin the application. If not, display an error message to user. That is what I'm attempting to do. Any ideas would be greatly appreciated. Shannon P.

        D 1 Reply Last reply
        0
        • S spettiford

          What I'm attempting to do is to validate what a user's id is within a textbox with what is stored within the sql server database. If the id on the textbox matches what is inside the database, begin the application. If not, display an error message to user. That is what I'm attempting to do. Any ideas would be greatly appreciated. Shannon P.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Christian gave you an idea that would work. What's the problem? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          S 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Christian gave you an idea that would work. What's the problem? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            S Offline
            S Offline
            spettiford
            wrote on last edited by
            #5

            My problem begins at "myCommand.ExecuteScalar()". My try...catch error handler reads, "Invalid object name: EmpId". My table has a column labeled "EmpId" so I'm not actually sure what the problem is. Any ideas?:) Shannon P. Dim myConnection As New System.Data.SqlClient.SqlConnection myConnection.ConnectionString = "workstation id=SHANNON-OUYIJ2M;packet size=4096;integrated security=SSPI;data source=SHANNON-OUYIJ2M;persist security info=False;initial catalog=WorkInstructionLibrary" 'Create a parameterised command that will return the number of matches. Dim myCommand As New SqlCommand("SELECT COUNT(*) FROM EmpID WHERE EmpID = @employeeId", myConnection) 'Set the parameter values to the credentials entered by the user. myCommand.Parameters.Add("@employeeId", SqlDbType.Int).Value = Me.txtEmpId.Text myConnection.Open() Try Dim count As Integer count = CInt(myCommand.ExecuteScalar()) If count = 0 Then 'generic fail message Else 'allow application to proceed End If Catch ex As SqlException Dim errorMessages As String Dim i As Integer For i = 0 To ex.Errors.Count - 1 errorMessages += "Index #" & i.ToString() & ControlChars.NewLine _ & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _ & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _ & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _ & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine Next i Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog log.Source = "My Application" log.WriteEntry(errorMessages) Console.WriteLine("An exception occurred. Please contact your system administrator.") MessageBox.Show("Error: EXECUTESCALAR METHOD" & errorMessages.ToString & " <> " & ex.ToString, "Error executing EXECUTESCALAR METHOD", MessageBoxButtons.OK) End Try

            S 1 Reply Last reply
            0
            • S spettiford

              My problem begins at "myCommand.ExecuteScalar()". My try...catch error handler reads, "Invalid object name: EmpId". My table has a column labeled "EmpId" so I'm not actually sure what the problem is. Any ideas?:) Shannon P. Dim myConnection As New System.Data.SqlClient.SqlConnection myConnection.ConnectionString = "workstation id=SHANNON-OUYIJ2M;packet size=4096;integrated security=SSPI;data source=SHANNON-OUYIJ2M;persist security info=False;initial catalog=WorkInstructionLibrary" 'Create a parameterised command that will return the number of matches. Dim myCommand As New SqlCommand("SELECT COUNT(*) FROM EmpID WHERE EmpID = @employeeId", myConnection) 'Set the parameter values to the credentials entered by the user. myCommand.Parameters.Add("@employeeId", SqlDbType.Int).Value = Me.txtEmpId.Text myConnection.Open() Try Dim count As Integer count = CInt(myCommand.ExecuteScalar()) If count = 0 Then 'generic fail message Else 'allow application to proceed End If Catch ex As SqlException Dim errorMessages As String Dim i As Integer For i = 0 To ex.Errors.Count - 1 errorMessages += "Index #" & i.ToString() & ControlChars.NewLine _ & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _ & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _ & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _ & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine Next i Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog log.Source = "My Application" log.WriteEntry(errorMessages) Console.WriteLine("An exception occurred. Please contact your system administrator.") MessageBox.Show("Error: EXECUTESCALAR METHOD" & errorMessages.ToString & " <> " & ex.ToString, "Error executing EXECUTESCALAR METHOD", MessageBoxButtons.OK) End Try

              S Offline
              S Offline
              spettiford
              wrote on last edited by
              #6

              Found the solution! My EmpId should have been my table name. Stupid syntax error on my part! Thanks to all. Shannon P.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups