Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   test session exist (http://www.velocityreviews.com/forums/t86472-test-session-exist.html)

Green 09-10-2004 06:27 PM

test session exist
 
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!

SevDer 09-10-2004 06:29 PM

Re: test session exist
 
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

Shiva 09-10-2004 06:30 PM

Re: test session exist
 
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" <someone@yahoo.com> wrote in message news:opsd4iwaxppk4u20@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!



Kevin Spencer 09-10-2004 06:43 PM

Re: test session exist
 
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" <shiva_sm@online.excite.com> wrote in message
news:uLJMxQ2lEHA.3988@tk2msftngp13.phx.gbl...
> 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" <someone@yahoo.com> wrote in message

news:opsd4iwaxppk4u20@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!
>
>




binuvar1989@gmail.com 09-21-2011 04:52 AM

check whether session name exists
 
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..


All times are GMT. The time now is 04:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.