If statement help...

c19h28O2

Member
Joined
Jul 24, 2006
Messages
6
Programming Experience
1-3
Hi,

Wondering if anyone can tell me what i'm doing wrong here...

VB.NET:
        Dim username As String
        Dim input As String
        sql = "select username from tbluserDetails where username = " & Me.txtUsername.Text
        da = New SqlClient.SqlDataAdapter(sql, myConn)
        da.Fill(ds, "tbluserDetails")
        username = ds.Tables("tbluserDetails").Rows("0").Item(0)
        input = Me.txtUsername.Text.ToString
        Convert.ToString(input)
        Convert.ToString(username)
        If input = username Then
            RegisterStartupScript("startupScript", "<script language=JavaScript>alert('Inside if statement' );</script>")
            Response.Redirect("default.aspx")
        End If

i've tested the sql code it is returning 3885909 and i've input that into the textbox but the if statement always results to false....

Cheers

C19
 
Perhaps i'm missing something simple here. I take it you have set a breakpoint and put the mouse
pointer over the two varaibles and looked to see if they equal the same thing? If they do i'd be suprised.
For strings i always use the equals fucntion...


If Username.Equals(Input) then
..
..
..
end if
 
Hi,

Unfortunately I can't, i'm using terminal services and my sys admin doesn't know why even though i am a member of the debuggers group can't actually debug! You don't realise how valuable debugging is unless you can't use it...!
 
VB.NET:
 username = ds.Tables("tbluserDetails").Rows("0").Item(0)

This line looks a bit wrong to me... The Rows("0") bit why are you using double quotes around the 0. The rows collection takes an integer as a index not a string. To me it looks like it should be....

VB.NET:
username = Convert.ToString(ds.Tables("tbluserDetails").Rows(0).Item(0))

Also here....

VB.NET:
input = Me.txtUsername.Text.ToString
Convert.ToString(input)

You have called the ToString method when the textbox returns a string anyway, and then you've converted it to a string again in the line below?.
 
Your code looks pretty crazy in some places..

SELECT username FROM users WHERE username = 'hello'

huh? youre only going to get 'hello' back? why would you select the username when you already know it?
if this is for login, then you should :

SELECT privileges FROM users WHERE username = blah and password = blah

now, if it's null, it was the wrong user/pass and if its a value you know what permissions the user has :)


-
If DS was a typed dataset, then you could say:
VB.NET:
If ds.tblUserDetails.Rows.Count > 0 Then
  Dim tudDR as MyTypedDataSet.tblUserDetailsRow = DirectCast(ds.tblUserDetails.Rows(0), MyTypedDataSet.tblUserDetailsRow)
 
  'do some stuff
  If txtUsername.Text = tudDR.USER_NAME Then ...
Else
  MessageBox.Show("User/Pass was wrong")
Endif


Finally, i'd like to point out that you appear to be programming ASP.net and you have posted this question in the VB.NET area of the forums. I'll report your post as needing moving to another section. Maybe someone can help with the debugging issue too
 
Back
Top