Need help with simple IF statement acting up

Stumped

New member
Joined
Mar 31, 2007
Messages
2
Programming Experience
Beginner
Ok it's probably not acting up and I've done something wrong, but I seem to be blind sighted completely in pinpointing the problem...

Here's a test code I was working on to make a simple login test:

VB.NET:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim user, pass, compare As String
        user = txtUser.Text
        pass = txtPass.Text.ToString
        conSQL.Open()
        passComSQL.CommandText = "select pass from admins where name = '" & user & "'"
        compare = passComSQL.ExecuteScalar.ToString
        conSQL.Close()
        Label1.Text = pass
        Label2.Text = compare
         If pass = compare Then
            MsgBox("Cool")
        Else
            MsgBox("boo")
        End If
    End Sub


I just created label1 and label2 to see if that indeed was the text within those string variables. And it was. I was a small table which is searched. I enter the correct username and password but I just keep getting "boo" in the message box.

conSQL and passComSQL are defined as well. Spot the problem?
 
TRy changing it to this:

VB.NET:
If pass.Equals(compare) Then ....
In addition, by default... it is case sensitive. IE, "ThisPassword" is not the same as "THISPASSWORD" ... the Equals has a second parameter that you can send in to tell it to ignore the case.

If that doesn't work, look at the Compare method of the String class....

-tg
 
post a screenshot, in png format, by attaching it to your post. Ensure the shot shows the label1 and label2 (i.e. dont cover them witha message box)
 
Did something and it worked...

In my DB I was using an nchar(10) field. And the password I was entering was just 6 chars long.

I switched the field length to 6 and it worked. Does SQL server pad up the field if its shorter? If so, with spaces? Left or right? Just asking since I intend to take it back to nchar(10) and pad up my string instead since people can have a varying length size in their passwords.

How do u guys go about building a login form?
 
for nchar and char fields... yes... those are fixed length fields..... if you want variable lengths, use nvarchar or varchar.

edit: if you .Trim what comes from SQL, the trailing spaces will be trimmed.

-tg
 
Back
Top