Session Object is Null in Page Load

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.
 
J

Jared

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.
 
J

Jared

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.
 
G

Guest

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top