Session is killing Me ?

  • Thread starter Thread starter Raghu Raman
  • Start date Start date
R

Raghu Raman

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.
 
Raghu said:
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")
.....
 
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 said:
Raghu said:
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.
 
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.
 
Back
Top