session object: simple question

  • Thread starter Thread starter Nikhil Patel
  • Start date Start date
N

Nikhil Patel

Hi all,
I am using a Session object in my ASP.Net application to store a value of
a Database field. I can access it as ...
int iProposalId = Session["ProposalId"];

The session timeout is set to 20. Now my question is if the session expires,
would the above statement produce any exception? If I can't find the
ProposalId value in the session object, I would like to display a message to
the user and close the window.

Thanks.
-Nikhil
 
Are you using any authentication? If you are then have that redirect to the
login page if it's expired, that's what it's designed for. If you aren't
just run a check
if(Session["blah"] is null)
 
int iProposalId = Session["ProposalId"];


you could then test: IsNothing(iProposalId)
 
Hi,
It will return null if the specified key doesn't exist in the session.

if (Session["ProposalId"] is null)
{
// Session expired.
}
else
{
// Parse
}

Hi all,
I am using a Session object in my ASP.Net application to store a value of
a Database field. I can access it as ...
int iProposalId = Session["ProposalId"];

The session timeout is set to 20. Now my question is if the session expires,
would the above statement produce any exception? If I can't find the
ProposalId value in the session object, I would like to display a message to
the user and close the window.

Thanks.
-Nikhil
 
Nikhil Patel said:
Hi all,
I am using a Session object in my ASP.Net application to store a value of
a Database field. I can access it as ...
int iProposalId = Session["ProposalId"];

The session timeout is set to 20. Now my question is if the session expires,
would the above statement produce any exception? If I can't find the
ProposalId value in the session object, I would like to display a message to
the user and close the window.

Thanks.
-Nikhil

If you are using C# then your statement shouldn't work: as Session
returns an object, you can't store it in an int. Solution: cast to int.

But: if that value doesn't exist (anymore) in the Session, you would
get a null value. You can't cast that to an int.

So your code should probably look like:

int iProposalId;
if (Session["ProposalId"] != null)
{
iProposalId = (int)Session["ProposalId"];
}
else
{
// show some alert ...
}



Hans Kesting
 
As a side note, you could see this as "handling the session timeout" rather
than just this particular value. If I remember a property of the session
object allows to test expiration...

Patrice

--

"Thomas Dodds" <[email protected]> a écrit dans le message de
int iProposalId = Session["ProposalId"];


you could then test: IsNothing(iProposalId)
 
The session timeout is set to 20. Now my question is if the session
expires,
would the above statement produce any exception? If I can't find the

It would throw a NullReferenceException. Rather than trying to catch the
exception, which is costly, why don't you check to see whether it is null
first?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Nikhil Patel said:
Hi all,
I am using a Session object in my ASP.Net application to store a value of
a Database field. I can access it as ...
int iProposalId = Session["ProposalId"];

The session timeout is set to 20. Now my question is if the session expires,
would the above statement produce any exception? If I can't find the
ProposalId value in the session object, I would like to display a message to
the user and close the window.

Thanks.
-Nikhil
 
Thank you all.
I use the following statements to check whether the ProposalID exists in
session object.

if (Session["ProposalId"] == null)

this.RegisterClientScriptBlock("Session
Expired","<script>OnSessionExpire()</script>");

else

{

int iProposalID = (int) Session["ProposalId"];

// use iProposalID.........

}

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top