6

I am comparing the session variable to a string to check if the login type is administrator or not.

Code i am using :

if (Session["loggedInUsername"] == null)
        {
            btnLogin.Text = "Sign In";
            lblWelcome.Text = "Welcome!";
            hypManageRestaurants.Enabled = false;
            hypManageReviews.Enabled = false;
            hypPostReviews.Enabled = false;

        }
        else
        {
            if (Session["loggedInUserType"] == "Administrator")
            {
                hypManageRestaurants.Enabled = true;
                hypManageReviews.Enabled = true;
                hypPostReviews.Enabled = true;
            }
            else
            {
                hypManageRestaurants.Enabled = false;
                hypManageReviews.Enabled = false;
                hypPostReviews.Enabled = true;
            }
            lblWelcome.Text = "Welcome " + Session["loggedInUsername"];

            btnLogin.Text = "Sign Out";
        }

So first I am checking if any user has logged in or not. If the user logs in successfully, the session variable "loggedInUsername" will have the value of the username. If the "loggedInUsername" session variable is not empty, it will check "loggedInUserType" session variable for the type of the logged in user.

Here comes the weird thing, the value of the "loggedInUserType" is exactly "Administrator" without the "", at the if function where I am comparing the session variable to the string "Administrator" is being skipped and goes to the else.

All session variables are getting values when the user logs in.

Below is the data which I am using to login and this record is the only record which have a type of "Administrator".

link to image

Is there any other method to compare a session variable to a string

drinu16
  • 775
  • 2
  • 11
  • 25
  • How do you mean it's being skipped? Is it going into the else statement? It's probably a casting issue. Try either Object.ReferenceEquals(a,b) or "Administrator".Equals(Session["..."]) – Thinking Sites May 21 '12 at 14:17
  • 1
    Use a [Membership provider](http://msdn.microsoft.com/en-us/library/yh26yfzy.aspx) instead. – Tim Schmelter May 21 '12 at 14:19

8 Answers8

5

Try

if(Convert.ToString(Session["loggedInUserType"]) == "Administrator) ...

Carl Winder
  • 938
  • 8
  • 18
5

Cast the object type value to a string

((string)Session["loggedInUserType"]) == "Administrator"
Jaimal Chohan
  • 8,530
  • 6
  • 43
  • 64
1

Try this:

if (Session["loggedInUserType"].ToString().Trim()
        .Equals("Administrator", StringComparison.InvariantCultureIgnoreCase))
Marco
  • 56,740
  • 14
  • 129
  • 152
1

The Session collection returns values of type Object, so when you compare that to a string you will be comparing the values of the object references, not comparing the string values.

Cast the object reference to string:

if (((string)Session["loggedInUserType"]) == "Administrator")
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Are you sure that all whitespace has been trimmed from the end of the Session["loggedInUserType"]?

Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
0
if (Session["loggedInUserType"].ToString() == "Administrator")
daryal
  • 14,643
  • 4
  • 38
  • 54
0

You can do this :

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

if (session == "Administrator")

or your Session can be in a specific class with getters.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
SarahB
  • 135
  • 1
  • 11
0
if(Convert.ToString(Session["loggedInUserType"]) == "Administrator)

In this way, no need to check null value becuase Convert.ToString handle Null value return "" empty string

Kashif Faraz
  • 321
  • 6
  • 19