[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2900
  • Last Modified:

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
0
jbrahy
Asked:
jbrahy
  • 2
1 Solution
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
if (null == Session.contents["name"])

or

if (null == Request["name"])
0
 
rbharany1Commented:
try putting it in a if block, e.g.
string name ="";

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now