Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# and ASP.Net Session Values

Status
Not open for further replies.

SteveL714

Programmer
Sep 19, 2013
21
US
I am in the process of converting an old VB.Net web-site I built over to C#. On the default page I define a series of Session values for reference on subsequent pages.

When I start to use them the ones that are predefined as string are available for reference. The ones that are predefined as some other type (i.e. integer, date) report the following error condition:

if (Session["WorkerID"] = 0)
Error: Cannot implicitly convert type 'object' to 'bool'. An explicit conversion exists (are you missing a cast)

I've Google'd the issue and so far nothing seems to address how to test the value of the WorkerId value as an integer. Can anyone help.

Steve
 
In contrast to VB, C# is very stringent when it comes to datatypes.
I mainly use C# for desktop apps, so I cannot say for sure but I would guess two things:
a) Instead of Session["WorkerID"] use Session["WorkerID"].ToString()
b) for comparisons, you need to use "==" (double equal). the single = is for value assignment only.
c) The session content is a string, 0 is an integer. Hence:
Code:
if(Session["Worker"].ToString() == "0")

Untested though.

Hope that helps!

MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
I tried MakeItSo's suggestion and it solfed my immediate problem.

I carried the logic a few lines further down in my code where I did a greater-than or equal to comparison and received another error.

if (Session["WorkerId"].ToString() >= "2") now gives me Error: Operator '>=' cannot be applied to operands of type 'string' and 'string'.

Ideally I would like to be able to do a numeric comparison. There has to be an obvious solution that I'm missing. Anybody have any ideas?

Steve

 
Well, simply put, DO a numerical comparison! :)
Either this way:
Code:
if(Session["WorkerId"].ToInt16() >= 2)

or in case this throws an error:
Code:
if(System.Convert.ToInt16(Session["WorkerId"].ToString()) >= 2)

Good luck!
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
That doesn't work. It appears that ToInt16 is an unrecognized operator.

'object' does not contain a definition for 'ToInt16' and no extension method 'Toin16' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?

What 'using directive' am I missing?

 
Did you try the second one too?
Try with ToInt32 instead.


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
ToInt32 is not available either. All I have is ToString, ToArray<>, and ToCharArray
 
Objects store in Session are exactly that, Objects, they are not strongly typed and unlike VB which lets you get away with murder, (as has been said), c# is a lot more stringent. Firstly, I would check that the Session object actually exists by performing a null check;

Code:
if(Session["WorkerId"] != null)

and alongside this I would explicitly cast the object to the datatype required to handle your explicit processing requirements, however, depending on those requirements you may either want to directly evaluate the value as part of the if statement or use a switch statement;
Code:
if(Session["WorkerId"] != null && Convert.ToInt32(Session["WorkerId"]) >= 2)
{
    // Do something...
}
or
Code:
if(Session["WorkerId"] != null)
{
    switch(Convert.ToInt32(Session["WorkerId"]))
    {
        case 0:
            // Do Something
            break;
        case 1:
            // Do Something
            break;
        default:
            // Do Something
            break;
    }
}


Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Did you get it to work??
Did you try with System.Convert as suggested several times?

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
System.Convert works, but I'm not sure why I had to do it that way. At least I'm not stuck any longer. Thanks all...

 
SteveL714, you have to do it that way because Objects store in SessionState are exactly that, Objects, they are not strongly typed

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top