none
If statement excutes else condition even if the primary condition is true RRS feed

  • Question

  • Hi, i am facing some problems with the if statement in vb.net.

    This is my code: Dim pass as string

    Dim objReader as New System.IO.StreamReader("C:\test.txt")

    pass=objReader.ReadtoEnd

    objReader.Close()

    If textbox1.text=pass then

    Msgbox("Access Granted")

    frmAccess.show

    Me.close()

    Else

    msgbox("Access Denied")

    textbox1.text=""

    End If

    Even if the condition textbox1.text=pass is true the if statement executes the else statement.

    Could somebody tell the cause for this nonrecognition of the if statement?

    Friday, April 19, 2013 5:39 AM

Answers

  • Before

       If textbox1.text=pass then

    Add this

            For x As Integer = 0 To textbox1.Text.Length - 1
                Debug.WriteLine(String.Format("{0} {1}", x, textbox1.Text(x)))
            Next

            Stop
            For x As Integer = 0 To pass.Length - 1
                Debug.WriteLine(String.Format("{0} {1}", x, pass(x)))
            Next
            Stop

    Are they equal?


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein


    • Edited by dbasnett Saturday, April 20, 2013 3:30 PM
    • Marked as answer by Royden Rego Tuesday, April 23, 2013 2:17 PM
    Saturday, April 20, 2013 3:30 PM

All replies

  • I can see possible two issues

    1. The string comparison is failing - Since you are reading from a text file, there might be some hidden character at the end of the data, like carriage return etc. Instead of using ReadtoEnd use ReadLine
    2. The code you have compiled might be an older version. Try to put a breakpoint in the If statement and check the value in each object by using the Watch window.

    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Proposed as answer by Cor Ligthert Friday, April 19, 2013 8:18 AM
    Friday, April 19, 2013 5:47 AM
  • use 

    if lcase(trim(textbo1.text)) = pass then

    It will eliminate spaces & Case errors.

    Saturday, April 20, 2013 11:06 AM
  • use 

    if lcase(trim(textbo1.text)) = pass then

    It will eliminate spaces & Case errors.

    Passwords are usually supposed to be case sensitive.
    Saturday, April 20, 2013 11:13 AM
  • Before

       If textbox1.text=pass then

    Add this

            For x As Integer = 0 To textbox1.Text.Length - 1
                Debug.WriteLine(String.Format("{0} {1}", x, textbox1.Text(x)))
            Next

            Stop
            For x As Integer = 0 To pass.Length - 1
                Debug.WriteLine(String.Format("{0} {1}", x, pass(x)))
            Next
            Stop

    Are they equal?


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein


    • Edited by dbasnett Saturday, April 20, 2013 3:30 PM
    • Marked as answer by Royden Rego Tuesday, April 23, 2013 2:17 PM
    Saturday, April 20, 2013 3:30 PM