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

Posted on 2007-10-02
Last Modified: 2008-01-09
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?


Question by:jbrahy
    LVL 17

    Accepted Solution

    if (null == Session.contents["name"])


    if (null == Request["name"])
    LVL 3

    Expert Comment

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

    if(Session.Contents["name"] != null)
        name = Session.Contents["name"].ToString();
    LVL 17

    Expert Comment

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

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now