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