Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Session object question!

Reply
Thread Tools

Session object question!

 
 
=?Utf-8?B?VG9ueQ==?=
Guest
Posts: n/a
 
      11-04-2004
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



 
Reply With Quote
 
 
 
 
Shiva
Guest
Posts: n/a
 
      11-04-2004
if (Session["abc"] == null)
{
// abc is null
}
else
{
// Use abc
}

"Tony" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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




 
Reply With Quote
 
 
 
 
Sacha
Guest
Posts: n/a
 
      11-04-2004
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" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
=?Utf-8?B?VG9ueQ==?=
Guest
Posts: n/a
 
      11-04-2004
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" <(E-Mail Removed)> a écrit dans le message de
> news:(E-Mail Removed)...
> > 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
> >
> >
> >

>
>
>

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      11-04-2004
> 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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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" <(E-Mail Removed)> a écrit dans le message de
> > news:(E-Mail Removed)...
> > > 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
> > >
> > >
> > >

> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Session Timeout problems-web.confg session state and IIS session s =?Utf-8?B?Um9iSEs=?= ASP .Net 4 04-11-2007 04:52 PM
Stored Session object is mixed up with another user's session object. momo898 ASP .Net 2 10-12-2006 03:33 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Session State - What does it take to establish one single ASP.NET session per "browser session" Jeff Smythe ASP .Net 3 01-02-2004 04:10 AM
How can I "know" the difference between a session timed out and a session that did session.abort? Jazzis ASP General 2 09-23-2003 07:16 AM



Advertisments