C# and ASP.Net Session Values
C# and ASP.Net Session Values
(OP)
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
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
RE: C# and ASP.Net Session Values
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
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.
RE: C# and ASP.Net Session Values
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
RE: C# and ASP.Net Session Values
Either this way:
CODE
or in case this throws an error:
CODE
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.
RE: C# and ASP.Net Session Values
'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?
RE: C# and ASP.Net Session Values
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.
RE: C# and ASP.Net Session Values
RE: C# and ASP.Net Session Values
CODE
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
CODE
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
RE: C# and ASP.Net Session Values
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.
RE: C# and ASP.Net Session Values
RE: C# and ASP.Net Session Values
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