Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to determine if a Session Variable exists?

Reply
Thread Tools

How to determine if a Session Variable exists?

 
 
Shapper
Guest
Posts: n/a
 
      06-08-2005
Hello,

How to determine if a Session Variable exists?

In Page_Load I need to check is Session("MyVar") Exists.
If it doesn't exist then I created it and give it a value:
Session("myVar") = "Value"

Thanks,
Miguel

 
Reply With Quote
 
 
 
 
Ziemowit
Guest
Posts: n/a
 
      06-08-2005
If variable doesn't exist then its null. Simlpy.

Ziemowit

 
Reply With Quote
 
 
 
 
Nawaz Ijaz
Guest
Posts: n/a
 
      06-08-2005
Simply do this..

if ( Session("MyVar") == "" )
{
Session("MyVar") = "Your Value"
}

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      06-08-2005
Ancient Zen problem:

If a Session variable is set, and there is no class to read it, does it
consume resources?

--


Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> How to determine if a Session Variable exists?
>
> In Page_Load I need to check is Session("MyVar") Exists.
> If it doesn't exist then I created it and give it a value:
> Session("myVar") = "Value"
>
> Thanks,
> Miguel
>



 
Reply With Quote
 
binuvar1989@gmail.com binuvar1989@gmail.com is offline
Junior Member
Join Date: Sep 2011
Posts: 2
 
      09-21-2011
Its difficult to check whether a session name exists or not..
Because we cannot use a session instance name before it is being declared, means assigning the value to it. So it is better to use the following code. It is very bad programming. but if you want you can use it.

try
{
string data = Session["sessionIds"].tostring();
}
catch(Exception ex)
{
// Do your handling code. ie, if you want to set the session value set here.
}


the idea behind is that using the session variable before declaring the value will thow an error. By this we can make sure that the session variable is not existing.
If you want more specific just check what error throwing in this scenario and check whether the error is same as you required error and do the handling code.
make sure to check whether the error is not coming from any other reasons.
Usually the error will be "object reference not set to an instance of the object".
Hope this will help some one..
 
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
System Session Variable VS. Own-declared 'Session' Variable chowchho ASP .Net 7 03-28-2008 02:38 PM
Session Timeout problems-web.confg session state and IIS session s =?Utf-8?B?Um9iSEs=?= ASP .Net 4 04-11-2007 04:52 PM
Determine a session variable type tshad ASP .Net 1 02-20-2006 07:11 AM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM
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



Advertisments