Link to home
Start Free TrialLog in
Avatar of jbrahy
jbrahy

asked on

what is the best way to test if a session key exists?

what is the best way to test if a session key exists?

string name = Session.Contents["name"].ToString();

that throws an exception if that value is not set.

What's the best practice in implementing this?

thanks,

John
ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rbharany1
rbharany1

try putting it in a if block, e.g.
string name ="";

if(Session.Contents["name"] != null)
{
    name = Session.Contents["name"].ToString();
}
more explicitly
if (null == Request["name"])
{
  //set a default value
  name = string.empty;
}
else
{name = Request["name"].ToString();}