Session Object is Null in Page Load

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Here's my code:

private void Page_Load(object sender, System.EventArgs e)
{
clientId = Session["clientId"].ToString();
.... etc.

It bombs on the assignment after the session has timed out
(System.NullReferenceException: Object reference not set to an instance of an
object). I don't understand why it is processing the page load event at all.
It should already have redirected, shouldn't it?

if I modify the code to do the redirection, it does not help and I get a 404:

private void Page_Load(object sender, System.EventArgs e)
{
if (Session["clientId"]==null)
{
Response.Redirect("Login.aspx",true);
}
}

The mechanism seems to be magical. I cannot debug it. Anyone any clue
what's going on?

TIA,
Jeb.
 
Have you tried putting the following line in Global.asax under
Session_Start?

Session.Add("clientID","");




Bruno said:
change this
if (Session["clientId"]==null)

to this
if ( String.IsEmptyOrNull( Session["clientId"] ) ) {


do you have sure that you assign the session("ClientId") before getting into
that page? So you can avoid the error...

--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)


JebBushell said:
Here's my code:

private void Page_Load(object sender, System.EventArgs e)
{
clientId = Session["clientId"].ToString();
... etc.

It bombs on the assignment after the session has timed out
(System.NullReferenceException: Object reference not set to an instanceof
an
object). I don't understand why it is processing the page load event at
all.
It should already have redirected, shouldn't it?

if I modify the code to do the redirection, it does not help and I get a
404:

private void Page_Load(object sender, System.EventArgs e)
{
if (Session["clientId"]==null)
{
Response.Redirect("Login.aspx",true);
}
}

The mechanism seems to be magical. I cannot debug it. Anyone any clue
what's going on?

TIA,
Jeb.
 
Have you tried putting the following line in Global.asax under
Session_Start?

Session.Add("clientID","");




Bruno said:
change this
if (Session["clientId"]==null)

to this
if ( String.IsEmptyOrNull( Session["clientId"] ) ) {


do you have sure that you assign the session("ClientId") before getting into
that page? So you can avoid the error...

--

Bruno Alexandre
(a Portuguese in Københanv, Danmark)


JebBushell said:
Here's my code:

private void Page_Load(object sender, System.EventArgs e)
{
clientId = Session["clientId"].ToString();
... etc.

It bombs on the assignment after the session has timed out
(System.NullReferenceException: Object reference not set to an instanceof
an
object). I don't understand why it is processing the page load event at
all.
It should already have redirected, shouldn't it?

if I modify the code to do the redirection, it does not help and I get a
404:

private void Page_Load(object sender, System.EventArgs e)
{
if (Session["clientId"]==null)
{
Response.Redirect("Login.aspx",true);
}
}

The mechanism seems to be magical. I cannot debug it. Anyone any clue
what's going on?

TIA,
Jeb.
 
There are two timeouts in my web.config file, one for session and another for
authentication. It seems that if the session times out before the
authentication then I get the problem. If this is the complete
explanation, then it seems like it's a standard Gotcha, and you might expect
to see warnings in documentation. Maybe I missed them?
 
Back
Top