Session object question!

Discussion in 'ASP .Net' started by =?Utf-8?B?VG9ueQ==?=, Nov 4, 2004.

  1. how do I check if the data in the session object is null before extracting it.
    The following throws an error if the Session object has not already been
    created.

    string str = System.Web.HttpContext.Current.Session["abc"].ToString();

    Thanks
    =?Utf-8?B?VG9ueQ==?=, Nov 4, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?VG9ueQ==?=

    Shiva Guest

    if (Session["abc"] == null)
    {
    // abc is null
    }
    else
    {
    // Use abc
    }

    "Tony" <> wrote in message
    news:...
    how do I check if the data in the session object is null before extracting
    it.
    The following throws an error if the Session object has not already been
    created.

    string str = System.Web.HttpContext.Current.Session["abc"].ToString();

    Thanks
    Shiva, Nov 4, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?VG9ueQ==?=

    Sacha Guest

    Hello...

    You have to use an Object :

    object o = System.Web.HttpContext.Current.Session["abc"] ;
    string str = string.Empty ;

    if (o != null)
    str = o.ToString() ;



    "Tony" <> a écrit dans le message de
    news:...
    > how do I check if the data in the session object is null before extracting

    it.
    > The following throws an error if the Session object has not already been
    > created.
    >
    > string str = System.Web.HttpContext.Current.Session["abc"].ToString();
    >
    > Thanks
    >
    >
    >
    Sacha, Nov 4, 2004
    #3
  4. Thanks - I new it would be an easy answer!!!


    "Sacha" wrote:

    > Hello...
    >
    > You have to use an Object :
    >
    > object o = System.Web.HttpContext.Current.Session["abc"] ;
    > string str = string.Empty ;
    >
    > if (o != null)
    > str = o.ToString() ;
    >
    >
    >
    > "Tony" <> a écrit dans le message de
    > news:...
    > > how do I check if the data in the session object is null before extracting

    > it.
    > > The following throws an error if the Session object has not already been
    > > created.
    > >
    > > string str = System.Web.HttpContext.Current.Session["abc"].ToString();
    > >
    > > Thanks
    > >
    > >
    > >

    >
    >
    >
    =?Utf-8?B?VG9ueQ==?=, Nov 4, 2004
    #4
  5. > Thanks - I new it would be an easy answer!!!

    Unfortunately, that was a klugy answer. You don't have to create an object
    variable. All you need to do is test if the value is null before attempting
    to assign it to a typed variable. Example:

    string str; // declare your string
    variable.
    if (Session["abc"] != null) // Check if Session reference is null
    str = (string) Session["abc"]; // Cast the Session object as a string
    (or whatever it is) and assign it

    This is much more efficient code. Also, it will work with any data type
    stored in Session. Just change the cast.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    I get paid good money to
    solve puzzles for a living


    "Tony" <> wrote in message
    news:D...
    > Thanks - I new it would be an easy answer!!!
    >
    >
    > "Sacha" wrote:
    >
    > > Hello...
    > >
    > > You have to use an Object :
    > >
    > > object o = System.Web.HttpContext.Current.Session["abc"] ;
    > > string str = string.Empty ;
    > >
    > > if (o != null)
    > > str = o.ToString() ;
    > >
    > >
    > >
    > > "Tony" <> a écrit dans le message de
    > > news:...
    > > > how do I check if the data in the session object is null before

    extracting
    > > it.
    > > > The following throws an error if the Session object has not already

    been
    > > > created.
    > > >
    > > > string str = System.Web.HttpContext.Current.Session["abc"].ToString();
    > > >
    > > > Thanks
    > > >
    > > >
    > > >

    > >
    > >
    > >
    Kevin Spencer, Nov 4, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Smythe
    Replies:
    3
    Views:
    1,238
    Jeff Smythe
    Jan 2, 2004
  2. Boban Dragojlovic
    Replies:
    2
    Views:
    468
    Patrice
    Apr 19, 2004
  3. momo898
    Replies:
    2
    Views:
    491
    bruce barker \(sqlwork.com\)
    Oct 12, 2006
  4. =?Utf-8?B?Um9iSEs=?=
    Replies:
    4
    Views:
    5,267
    =?Utf-8?B?Um9iSEs=?=
    Apr 11, 2007
  5. Jazzis
    Replies:
    2
    Views:
    242
    Jazzis
    Sep 23, 2003
Loading...

Share This Page