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
Session variables are special named "containers" for values that may change during the installation process. At run time these variables are automatically replaced with the string of text that is assigned to them.
You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.
To access the session variable which we have defined in our ASP.NET or C# language, we just have to pass that variable name as it is inside this <%= %> tag as <%= Session["UserName"] %> . Note that this tag is enclosed inside a string.
Session Variables- The session variables are variables maintained on server side by asp.net runtime. Each user is identified by a a unique number called SessioID. This session is stored in a cookie (if browser supports cookie) on client side after the first user request.
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: You can do this : or your Session can be in a specific class with getters.
Now, follow the below steps to compare strings in VBA. Step 1: Define sub-procedure which can hold your macro. Step 2: Define a variable Result as String so that we can assign a value of StrComp function to it. Step 3: Now use the Assignment operator to assign the value of StrComp to the variable named Result.
There are three types of comparison mentioned below: vbBinaryCompare: Compares two strings binarily and is case sensitive (‘UPPER’ does not equal to ‘upper’). This is the default comparison method if not specified. vbTextCompare: Compares two strings as texts and is not case sensitive (‘Upper’ equals to ‘upper’).
vbTextCompare: Compares two strings as texts and is not case sensitive (‘Upper’ equals to ‘upper’). vbDatabaseCompare: Compares the text through the database and is only available for Microsoft Access not under VBA. This function can return three possible results as follows:
Cast the object type value to a string
((string)Session["loggedInUserType"]) == "Administrator"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With