Marly
Marly

Reputation: 51

If statement does not work?

it always go with else, even if txtUserName.Text = "Adm_A" Why is that ?

If txtUserName.Text Like "Adm_?" Then
            Response.Redirect("AdminLoggedIn.aspx")
        Else
            Response.Redirect("LoggedIn.aspx")
        End If

Upvotes: 2

Views: 116

Answers (5)

Leopold Stotch
Leopold Stotch

Reputation: 1522

How about checking if it starts with the "Adm_" string:

If txtUserName.Text.StartsWith("Adm_") Then
        Response.Redirect("AdminLoggedIn.aspx")
    Else
        Response.Redirect("LoggedIn.aspx")
    End If

Upvotes: 0

Steve
Steve

Reputation: 216273

A simple console application to test your problem

Sub Main
    Dim test = "Adm_AA"
    CheckLike(test)
    test = "Adm_A"
    CheckLike(test)
    test = "Adm_A "   ' a space after the A'
    CheckLike(test)
End Sub

Sub CheckLike(ByVal toCheck as string)

    If toCheck Like "Adm_?" Then
        Console.WriteLine("Matched")
    Else
        Console.WriteLine("Not Matched")
    End If
End Sub

Results:

Not Matched
Matched
Not Matched

So there is something in your strings that it's not like you think it is. Use the debugger and verify the if statement and its argument. Also pay attention to the locale of the server where you run the webapp and the Option Compare used

Upvotes: 1

DevelopmentIsMyPassion
DevelopmentIsMyPassion

Reputation: 3591

can you try this

If txtUserName.Text.Trim() Like "Adm?" Then

Upvotes: 0

Taicho
Taicho

Reputation: 143

I believe that is because the question mark is a pattern operator. Please check http://msdn.microsoft.com/en-us/library/swf8kaxw(v=vs.71).aspx

Have you tried using contains instead of like?

Upvotes: 0

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Try this:

If txtUserName.Text.Trim() Like "Adm_?" Then

Upvotes: 0

Related Questions