Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

Test if Session Variable Exists!!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adam J Knight - 17 Feb 2006 09:48 GMT
Hi all,

I am trying to test to see if a Session variable exists.

This was my initial attempt, and doesn't work
int intInstructorID = (Session["InstructorID"].ToString() == null ?
Convert.ToInt32(Request.QueryString["InstructorID"]):
Convert.ToInt32(Session["InstructorID"]));

Can anyone give me a head up on the best way to do this??

Cheers,
Adam
kahtava@gmail.com - 17 Feb 2006 10:24 GMT
You've got it..

Perhaps avoid the use of the ternary operator for readability sake, but
then again you may get better performance from short circuiting the
ternary if statement.

I would suggest sticking to either session variables or query strings;
in the end it makes life easier.

if( Session["InstructorID"] != null ){
 Convert.ToInt32(Request.QueryString["InstructorID"]):
}
else{
 Convert.ToInt32(Request.QueryString["InstructorID"]):
}

I found that through URL ReWriting I could get the functionality I
desired with session variables through the use of query strings.

Cheers,
-Adam
Ravi Ambros Wallau - 17 Feb 2006 12:32 GMT
Perhaps:

if( Session["InstructorID"] != null ){
Convert.ToInt32(Session["InstructorID"]):
}
else{
Convert.ToInt32(Request.QueryString["InstructorID"]):
}

> You've got it..
>
[quoted text clipped - 17 lines]
> Cheers,
> -Adam
Hans Kesting - 17 Feb 2006 11:32 GMT
> Hi all,
>
[quoted text clipped - 9 lines]
> Cheers,
> Adam

The test 'Session["InstructorID"].ToString() == null' will fail
if Session["InstructorID"] is null, because then there is nothing to
call ToString() on. Remove the ToString and it should work (*if* you
are sure that either the Session or the QueryString delivers something
that can be converted into string).

int intInstructorID = (Session["InstructorID"] == null ?
Convert.ToInt32(Request.QueryString["InstructorID"]):
Convert.ToInt32(Session["InstructorID"]));

Hans Kesting

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.