Test if Session Variable Exists!!


A

Adam J Knight

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
 
Ad

Advertisements

K

kahtava

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
 
H

Hans Kesting

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

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
 
Ad

Advertisements

R

Ravi Ambros Wallau

Perhaps:

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

Top