Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am pulling username and password from a database and comparing to values entered into a text box.

Here is the code I'm working on:
VB
    Dim con As System.Data.SqlClient.SqlConnection
    Dim cmd As System.Data.SqlClient.SqlCommand
    Dim dr As System.Data.SqlClient.SqlDataReader
    Dim sqlStr As String
    Dim username As String
    Dim password As String
    Dim userid As Integer
    Dim testme As String

Private Sub LoginTest ()
       If usernamebox.Text.Length = 0 Then
            MsgBox("You must enter a username")
            usernamebox.Focus()
            Exit Sub
        ElseIf passwordbox.Text.Length = 0 Then
            MsgBox("You must enter a password")
            passwordbox.Focus()
            Exit Sub
        End If

        con = New SqlClient.SqlConnection(Module1.GetConnectionString())
        con.Open()
        sqlStr = "Select userid, username, password from users where username = '" & usernamebox.Text & "';"
        cmd = New SqlClient.SqlCommand(sqlStr, con)
        dr = cmd.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            userid = dr.Item("userid")
            username = dr.Item("username")
            password = dr.Item("password")
        Else
            MsgBox("Invalid Login Name")
            Exit Sub
        End If


        If passwordbox.Text = password Then
            MsgBox("Show roles Page")
        Else
            passwordbox.Text = ""
            ' The user name/password is invalid.
            MsgBox("Password invalid")
            passwordbox.Focus()
            Exit Sub
        End If

        MsgBox("Show roles Page")



        con.Close()
End Sub

It always fails the comparison - if passwordbox.text = password - even if they are correct. In VB studio in debug mode I hover over the password and it shows a double quote before the actual password like this: lets say the password is mypassword then the hover over of the value pulled from the db is "mypassword and the value from hover over on the textbox is "mypassword".

What am I doing wrong?
Posted
Updated 7-Nov-14 10:57am
v2

1 solution

does the password in the database contain trailing spaces perhaps ? - I have a feeling its the VB environment that displays the double quotes around the value, so the only reason I can think of to see the left one and not the right one, is, the right one is 'off the field of view'

do a select of data from the database, append something like '[' and ']' around the password value, and check - you may need to update the password column and 'trim' extraneous spaces (or other sh*t)

[edit] you could try

Select userid, username, rtrim(password) from users where username = 


in your sql (note the rtrim() around the password - trims whitespace on the right of the string)

If it works, well .. obviously the best answer is not to have the whitespace there in the first place, but .....

[/edit]
 
Share this answer
 
v2
Comments
Member 11215976 10-Nov-14 9:10am    
Thanks Garth,

That was the problem. Simple solutions often escape me, to busy looking for more complex problem.

Now I'll go see why the password had the extra spaces to begin with.
Member 11215976 10-Nov-14 9:25am    
Problem found. For some reason I defined the table columns as nchar instead of nvarchar. Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900