By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Session is killing Me ?

P: n/a
Hi,

am in c#.net project.

am using the session object for some logic.
e-g:session["Mode"]="Add";
i can refer this by using
if(Session["Mode"]=="Add")
{
logic
}

It is working nice for some moment.later on ,it will tell the error
like(in if statement)
------------------------------------------
'object referrence not set to an instance of an object'
------------------------------------------------------
if i change the 'if' statement as

if(Session["Mode"].Tostring()=="Add")
{
logic
}

it is working nice for some moment . and again tells the same error .So
iam keep on changing between the statements as
i given above & get working.

Why it is killing Me like that.What is the exact statement / Have i to
include something ?
Please help me

With thanks
a drop in the ocean.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Raghu Raman wrote:
Hi,

am in c#.net project.

am using the session object for some logic.
e-g:session["Mode"]="Add";
i can refer this by using
if(Session["Mode"]=="Add")
{
logic
}

Does this even compile? Session["Mode"] should return an Object.
If it is possible to compare with a string, then the result could be unexpected.
It is working nice for some moment.later on ,it will tell the error
like(in if statement)
------------------------------------------
'object referrence not set to an instance of an object'
------------------------------------------------------
if i change the 'if' statement as

if(Session["Mode"].Tostring()=="Add")
{
logic
}

What if there is no ' Session["Mode"] ' ? It would return null then,
and null.ToString() will give that exception.
Solution: use this check:
if (Session["Mode"] != null && Session["Mode"].ToString() == "Add")
.....
it is working nice for some moment . and again tells the same error
.So iam keep on changing between the statements as
i given above & get working.

Why it is killing Me like that.What is the exact statement / Have i to
include something ?
Please help me

With thanks
a drop in the ocean.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 19 '05 #2

P: n/a
First of all you should follow what Hans Kesting said about checking for the
existence of the Session variable. Now, if you are losing your session data
after sometime, then you got a bigger problem. Try to debug and find out if
Session_End event handler is firing before your session timeout occurs.
There can be so many reasons why that could happen. So, first try to figure
out if your session is dying before the timeout occurs

--
Kumar Reddi
http://kumarreddi.blogspot.com

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:OM**************@TK2MSFTNGP09.phx.gbl...
Raghu Raman wrote:
Hi,

am in c#.net project.

am using the session object for some logic.
e-g:session["Mode"]="Add";
i can refer this by using
if(Session["Mode"]=="Add")
{
logic
}

Does this even compile? Session["Mode"] should return an Object.
If it is possible to compare with a string, then the result could be

unexpected.
It is working nice for some moment.later on ,it will tell the error
like(in if statement)
------------------------------------------
'object referrence not set to an instance of an object'
------------------------------------------------------
if i change the 'if' statement as

if(Session["Mode"].Tostring()=="Add")
{
logic
}


What if there is no ' Session["Mode"] ' ? It would return null then,
and null.ToString() will give that exception.
Solution: use this check:
if (Session["Mode"] != null && Session["Mode"].ToString() == "Add")
....
it is working nice for some moment . and again tells the same error
.So iam keep on changing between the statements as
i given above & get working.

Why it is killing Me like that.What is the exact statement / Have i to
include something ?
Please help me

With thanks
a drop in the ocean.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 19 '05 #3

P: n/a
Sessions time out, so you should always check for null before trying to work
with any object stored in Session. You should also have a way of restoring
items which are discarded due to timeout.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"Raghu Raman" <ra************@rediffmail.com> wrote in message
news:#U**************@TK2MSFTNGP10.phx.gbl...
Hi,

am in c#.net project.

am using the session object for some logic.
e-g:session["Mode"]="Add";
i can refer this by using
if(Session["Mode"]=="Add")
{
logic
}

It is working nice for some moment.later on ,it will tell the error
like(in if statement)
------------------------------------------
'object referrence not set to an instance of an object'
------------------------------------------------------
if i change the 'if' statement as

if(Session["Mode"].Tostring()=="Add")
{
logic
}

it is working nice for some moment . and again tells the same error .So
iam keep on changing between the statements as
i given above & get working.

Why it is killing Me like that.What is the exact statement / Have i to
include something ?
Please help me

With thanks
a drop in the ocean.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.