Validating textbox value against a db
-
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.
-
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.
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++
-
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++
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.
-
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.
Christian gave you an idea that would work. What's the problem? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Christian gave you an idea that would work. What's the problem? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
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
-
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
Found the solution! My EmpId should have been my table name. Stupid syntax error on my part! Thanks to all. Shannon P.