statmaster
statmaster

Reputation: 1219

How to compare a session variable with a string in C#?

i edited the question

it still does not work, the user writes appendix then press OK in Login, nothing happens

here is the login (vb.net)

Partial Class login
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Session("passcode") = TextBox1.Text
    Response.Redirect("Default.aspx")

End Sub
End Class

and here is the default page C#

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["passcode"] == "appendix")
    {
       Response.Write("OK !");
    }
    else
    {
        Response.Redirect("login.aspx");
    }
}


}

Upvotes: 1

Views: 8015

Answers (2)

user9371102
user9371102

Reputation: 1276

Cast the object type value to a string

((string)Session["loggedInUserType"]) == "Administrator"

Refer here

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422162

You probably mean

Session["passcode"] == "Appendix"

In C# (unlike VB), == is the equality operator and = is the assignment operator.

Upvotes: 9

Related Questions