Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > test session exist

Reply
Thread Tools

test session exist

 
 
Green
Guest
Posts: n/a
 
      09-10-2004
Hi,
I want to test whether a sesion exist, what can i do?
I use Session["name"].toString().length == 0 , but it will prompt the
error saying object is not referenced. How can i test Session["name"]
exist?

Thanks in advance!
 
Reply With Quote
 
 
 
 
SevDer
Guest
Posts: n/a
 
      09-10-2004
Green wrote:

> Hi,
> I want to test whether a sesion exist, what can i do?
> I use Session["name"].toString().length == 0 , but it will prompt
> the error saying object is not referenced. How can i test
> Session["name"] exist?
>
> Thanks in advance!


Test it against null.
And as in your example, when it is null, it will complain with the given
error message.

--

SevDer
http://www.sevder.com
 
Reply With Quote
 
 
 
 
Shiva
Guest
Posts: n/a
 
      09-10-2004
First, check for the existance of the item in the session state collection
before accessing its properties:

if (Session["name"] == null)
{
// name does not exist in the Session state collection.
}
else
{
// Access Session["name"] here...
}

Hope I got the question correctly.

"Green" <(E-Mail Removed)> wrote in message newspsd4iwaxppk4u20@dev-02...
Hi,
I want to test whether a sesion exist, what can i do?
I use Session["name"].toString().length == 0 , but it will prompt the
error saying object is not referenced. How can i test Session["name"]
exist?

Thanks in advance!


 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      09-10-2004
And BTW, on the server side, the Session ALWAYS exists - unless it takes
longer than 20 minutes for the Page to process. Just want to make sure
there's no confusion between the Session Collection and its members.

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

"Shiva" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> First, check for the existance of the item in the session state collection
> before accessing its properties:
>
> if (Session["name"] == null)
> {
> // name does not exist in the Session state collection.
> }
> else
> {
> // Access Session["name"] here...
> }
>
> Hope I got the question correctly.
>
> "Green" <(E-Mail Removed)> wrote in message

newspsd4iwaxppk4u20@dev-02...
> Hi,
> I want to test whether a sesion exist, what can i do?
> I use Session["name"].toString().length == 0 , but it will prompt the
> error saying object is not referenced. How can i test Session["name"]
> exist?
>
> Thanks in advance!
>
>



 
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
Does my object exist? So why its HWND doesn't exist? That's a question... (CMonthCalCtrl control) LT C++ 7 07-25-2004 07:08 PM
Can the Favourites Sidebar (IE, Opera, etc.) be addressed selectively (exist or non-exist)? Markus Mohr Javascript 7 11-28-2003 12:20 AM
In Schema, how to say "If one element exist, another element must exist"? Y.S. XML 3 09-17-2003 02:51 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments