INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

RE: C# and ASP.Net Session Values

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.

RE: C# and ASP.Net Session Values

(OP)
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

RE: C# and ASP.Net Session Values

Well, simply put, DO a numerical comparison! smile
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.

RE: C# and ASP.Net Session Values

(OP)
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?

RE: C# and ASP.Net Session Values

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.

RE: C# and ASP.Net Session Values

(OP)
ToInt32 is not available either. All I have is ToString, ToArray<>, and ToCharArray

RE: C# and ASP.Net Session Values

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

RE: C# and ASP.Net Session Values

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.

RE: C# and ASP.Net Session Values

(OP)
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...

RE: C# and ASP.Net Session Values

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close