Session State null at compile

R

RichB

I have an odd problem which has me stumped around using Session state.

I have an entirely dynamic page, with all controls added within the
codebehind file. I save various objects to session state so that I can
recreate the page on postback. This all works fine on second running, but on
first run after making a change to the code, I get a Null ReferenceException
on a variable set from the session using e.g. imageSelected =
(string)Session["imageSelected"];

If I select restart and take exactly the same steps, then the reference is
set correctly and all is well.

Is there any simple explanation? or is more information required?

Thanks,
Richard
 
R

RichB

That is fine, I am not expecting it to be persisted between restarts, I just
expect that when I first run the application and save to Session state, that
when I retreive those variables that they will be there without having to
stop and restart.

However having looked at it further, it just gets more strange. I have
therefore posted my code below. Ihave set a breakpoint at the line: if
(!IsPostback). If after adding a comment in this file I run the code and at
the breakpoint select continue and then select an image to cause a postback
and on hitting the breakpoint select continue again, I get the
NullReferenceException at the line: foreach (Photo photo in
photos.PhotoCollection). However if instead of selecting continue in the
debugger, I step through the code the Session variables get set and can then
be retreived with no problem atall. I am certain that this is happening, I
added a comment, steped through and it worked Ok, then removed the comment
and selected to continue and received the error.

Thanks, Richard
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using FlickrNet;

using System.IO;

using System.Net;

using System.Collections.Generic;





namespace FlickrFriends

{

public partial class SetImages : System.Web.UI.Page

{

Flickr flickr;

Photoset photos;

string imageSelected;

string linkSelected;

List<Photo> downPhotos = new List<Photo>();

protected void Page_Load(object sender, EventArgs e)

{


if (!IsPostBack)

{

flickr = PreviousPage.flickr;

Session["flickr"] = flickr ;


this.linkSelected = PreviousPage.linkSelected;

if (linkSelected != null)

{

photos = flickr.PhotosetsGetPhotos(linkSelected);

Session["photos"] = photos;

}

else throw new Exception ("link selected is null");


}

else

{

this.flickr = (Flickr)Session["flickr"];

this.photos = (Photoset)Session["photos"];



imageSelected = (string)Session["imageSelected"];

}


foreach (Photo photo in photos.PhotoCollection)

{

ImageButton image = new ImageButton();

CheckBox chk = new CheckBox();

HtmlGenericControl span = new HtmlGenericControl("span");


form1.Controls.Add(span);

span.Controls.Add(image);

span.Controls.Add(chk);



image.ID = photo.PhotoId;

chk.ID = "chk_"+photo.PhotoId;

chk.CheckedChanged += new EventHandler(checkBox_checkChanged);



image.Command += new CommandEventHandler(image_Click);

image.CommandName = photo.PhotoId;

image.ImageUrl = photo.SquareThumbnailUrl;

}






}

protected void image_Click(object sender, CommandEventArgs e)

{


foreach (Photo p in photos.PhotoCollection)

{

if (p.PhotoId == e.CommandName)

{

((Image)sender).ImageUrl = p.SmallUrl;

}

}


Session["imageSelected"] = ((Image)sender).ID;

Trace.Write("selected image: "+ (string)Session["imageSelected"]);

}


Peter Bromberg said:
When you "make a change to the code", e.g. modify any file in the
application
root, your application will restart. Session, Cache and Application state
all
go away at that point.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


RichB said:
I have an odd problem which has me stumped around using Session state.

I have an entirely dynamic page, with all controls added within the
codebehind file. I save various objects to session state so that I can
recreate the page on postback. This all works fine on second running, but
on
first run after making a change to the code, I get a Null
ReferenceException
on a variable set from the session using e.g. imageSelected =
(string)Session["imageSelected"];

If I select restart and take exactly the same steps, then the reference
is
set correctly and all is well.

Is there any simple explanation? or is more information required?

Thanks,
Richard
 
B

bruce barker

if you edit your code, this is a restart.

you need to code for null sessions, as they will happen on your productions
server. lots of events can cause a restart and session loss. (too much memory
use, too long idle, iisreset, code drop, etc).


-- bruce (sqlwork.com)


RichB said:
That is fine, I am not expecting it to be persisted between restarts, I just
expect that when I first run the application and save to Session state, that
when I retreive those variables that they will be there without having to
stop and restart.

However having looked at it further, it just gets more strange. I have
therefore posted my code below. Ihave set a breakpoint at the line: if
(!IsPostback). If after adding a comment in this file I run the code and at
the breakpoint select continue and then select an image to cause a postback
and on hitting the breakpoint select continue again, I get the
NullReferenceException at the line: foreach (Photo photo in
photos.PhotoCollection). However if instead of selecting continue in the
debugger, I step through the code the Session variables get set and can then
be retreived with no problem atall. I am certain that this is happening, I
added a comment, steped through and it worked Ok, then removed the comment
and selected to continue and received the error.

Thanks, Richard
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using FlickrNet;

using System.IO;

using System.Net;

using System.Collections.Generic;





namespace FlickrFriends

{

public partial class SetImages : System.Web.UI.Page

{

Flickr flickr;

Photoset photos;

string imageSelected;

string linkSelected;

List<Photo> downPhotos = new List<Photo>();

protected void Page_Load(object sender, EventArgs e)

{


if (!IsPostBack)

{

flickr = PreviousPage.flickr;

Session["flickr"] = flickr ;


this.linkSelected = PreviousPage.linkSelected;

if (linkSelected != null)

{

photos = flickr.PhotosetsGetPhotos(linkSelected);

Session["photos"] = photos;

}

else throw new Exception ("link selected is null");


}

else

{

this.flickr = (Flickr)Session["flickr"];

this.photos = (Photoset)Session["photos"];



imageSelected = (string)Session["imageSelected"];

}


foreach (Photo photo in photos.PhotoCollection)

{

ImageButton image = new ImageButton();

CheckBox chk = new CheckBox();

HtmlGenericControl span = new HtmlGenericControl("span");


form1.Controls.Add(span);

span.Controls.Add(image);

span.Controls.Add(chk);



image.ID = photo.PhotoId;

chk.ID = "chk_"+photo.PhotoId;

chk.CheckedChanged += new EventHandler(checkBox_checkChanged);



image.Command += new CommandEventHandler(image_Click);

image.CommandName = photo.PhotoId;

image.ImageUrl = photo.SquareThumbnailUrl;

}






}

protected void image_Click(object sender, CommandEventArgs e)

{


foreach (Photo p in photos.PhotoCollection)

{

if (p.PhotoId == e.CommandName)

{

((Image)sender).ImageUrl = p.SmallUrl;

}

}


Session["imageSelected"] = ((Image)sender).ID;

Trace.Write("selected image: "+ (string)Session["imageSelected"]);

}


Peter Bromberg said:
When you "make a change to the code", e.g. modify any file in the
application
root, your application will restart. Session, Cache and Application state
all
go away at that point.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


RichB said:
I have an odd problem which has me stumped around using Session state.

I have an entirely dynamic page, with all controls added within the
codebehind file. I save various objects to session state so that I can
recreate the page on postback. This all works fine on second running, but
on
first run after making a change to the code, I get a Null
ReferenceException
on a variable set from the session using e.g. imageSelected =
(string)Session["imageSelected"];

If I select restart and take exactly the same steps, then the reference
is
set correctly and all is well.

Is there any simple explanation? or is more information required?

Thanks,
Richard
 
R

RichB

Sorry, I'm struggling to understand this. I get that the application has
restarted, and that therefore the session state variables in the current
session are lost. However what I do not understand is why when I next try to
set the variables in the session they do not get set unless I step through
the code, but if I restart the application again then session variables are
stored fine.

Sure the session will be lost and the app needs to handle this, but I just
do not understand why in this particular circumstance the session variables
are not being set.

I seem to have solved the problem by saving a session variable on the first
page of the application (which has no need to use Session state). This just
seems odd to me.....

I'd still like to understand this, and whether I always need to employ this
work around, or if there is another solution.

Thanks,
Richard


bruce barker said:
if you edit your code, this is a restart.

you need to code for null sessions, as they will happen on your
productions
server. lots of events can cause a restart and session loss. (too much
memory
use, too long idle, iisreset, code drop, etc).


-- bruce (sqlwork.com)


RichB said:
That is fine, I am not expecting it to be persisted between restarts, I
just
expect that when I first run the application and save to Session state,
that
when I retreive those variables that they will be there without having to
stop and restart.

However having looked at it further, it just gets more strange. I have
therefore posted my code below. Ihave set a breakpoint at the line: if
(!IsPostback). If after adding a comment in this file I run the code and
at
the breakpoint select continue and then select an image to cause a
postback
and on hitting the breakpoint select continue again, I get the
NullReferenceException at the line: foreach (Photo photo in
photos.PhotoCollection). However if instead of selecting continue in the
debugger, I step through the code the Session variables get set and can
then
be retreived with no problem atall. I am certain that this is happening,
I
added a comment, steped through and it worked Ok, then removed the
comment
and selected to continue and received the error.

Thanks, Richard
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using FlickrNet;

using System.IO;

using System.Net;

using System.Collections.Generic;





namespace FlickrFriends

{

public partial class SetImages : System.Web.UI.Page

{

Flickr flickr;

Photoset photos;

string imageSelected;

string linkSelected;

List<Photo> downPhotos = new List<Photo>();

protected void Page_Load(object sender, EventArgs e)

{


if (!IsPostBack)

{

flickr = PreviousPage.flickr;

Session["flickr"] = flickr ;


this.linkSelected = PreviousPage.linkSelected;

if (linkSelected != null)

{

photos = flickr.PhotosetsGetPhotos(linkSelected);

Session["photos"] = photos;

}

else throw new Exception ("link selected is null");


}

else

{

this.flickr = (Flickr)Session["flickr"];

this.photos = (Photoset)Session["photos"];



imageSelected = (string)Session["imageSelected"];

}


foreach (Photo photo in photos.PhotoCollection)

{

ImageButton image = new ImageButton();

CheckBox chk = new CheckBox();

HtmlGenericControl span = new HtmlGenericControl("span");


form1.Controls.Add(span);

span.Controls.Add(image);

span.Controls.Add(chk);



image.ID = photo.PhotoId;

chk.ID = "chk_"+photo.PhotoId;

chk.CheckedChanged += new EventHandler(checkBox_checkChanged);



image.Command += new CommandEventHandler(image_Click);

image.CommandName = photo.PhotoId;

image.ImageUrl = photo.SquareThumbnailUrl;

}






}

protected void image_Click(object sender, CommandEventArgs e)

{


foreach (Photo p in photos.PhotoCollection)

{

if (p.PhotoId == e.CommandName)

{

((Image)sender).ImageUrl = p.SmallUrl;

}

}


Session["imageSelected"] = ((Image)sender).ID;

Trace.Write("selected image: "+ (string)Session["imageSelected"]);

}


message
When you "make a change to the code", e.g. modify any file in the
application
root, your application will restart. Session, Cache and Application
state
all
go away at that point.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


:

I have an odd problem which has me stumped around using Session state.

I have an entirely dynamic page, with all controls added within the
codebehind file. I save various objects to session state so that I can
recreate the page on postback. This all works fine on second running,
but
on
first run after making a change to the code, I get a Null
ReferenceException
on a variable set from the session using e.g. imageSelected =
(string)Session["imageSelected"];

If I select restart and take exactly the same steps, then the
reference
is
set correctly and all is well.

Is there any simple explanation? or is more information required?

Thanks,
Richard
 
P

Peter Bromberg [C# MVP]

You get unusual behaving with various kinds of things like this when running
in debug mode inside the IDE. Run your code in release mode on a real web
site without Visual Studio involved. If you need to find out whether it is
still happening, write some simple logging code that you can easily turn on
or off via a boolean appSettings element.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


RichB said:
Sorry, I'm struggling to understand this. I get that the application has
restarted, and that therefore the session state variables in the current
session are lost. However what I do not understand is why when I next try to
set the variables in the session they do not get set unless I step through
the code, but if I restart the application again then session variables are
stored fine.

Sure the session will be lost and the app needs to handle this, but I just
do not understand why in this particular circumstance the session variables
are not being set.

I seem to have solved the problem by saving a session variable on the first
page of the application (which has no need to use Session state). This just
seems odd to me.....

I'd still like to understand this, and whether I always need to employ this
work around, or if there is another solution.

Thanks,
Richard


bruce barker said:
if you edit your code, this is a restart.

you need to code for null sessions, as they will happen on your
productions
server. lots of events can cause a restart and session loss. (too much
memory
use, too long idle, iisreset, code drop, etc).


-- bruce (sqlwork.com)


RichB said:
That is fine, I am not expecting it to be persisted between restarts, I
just
expect that when I first run the application and save to Session state,
that
when I retreive those variables that they will be there without having to
stop and restart.

However having looked at it further, it just gets more strange. I have
therefore posted my code below. Ihave set a breakpoint at the line: if
(!IsPostback). If after adding a comment in this file I run the code and
at
the breakpoint select continue and then select an image to cause a
postback
and on hitting the breakpoint select continue again, I get the
NullReferenceException at the line: foreach (Photo photo in
photos.PhotoCollection). However if instead of selecting continue in the
debugger, I step through the code the Session variables get set and can
then
be retreived with no problem atall. I am certain that this is happening,
I
added a comment, steped through and it worked Ok, then removed the
comment
and selected to continue and received the error.

Thanks, Richard
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using FlickrNet;

using System.IO;

using System.Net;

using System.Collections.Generic;





namespace FlickrFriends

{

public partial class SetImages : System.Web.UI.Page

{

Flickr flickr;

Photoset photos;

string imageSelected;

string linkSelected;

List<Photo> downPhotos = new List<Photo>();

protected void Page_Load(object sender, EventArgs e)

{


if (!IsPostBack)

{

flickr = PreviousPage.flickr;

Session["flickr"] = flickr ;


this.linkSelected = PreviousPage.linkSelected;

if (linkSelected != null)

{

photos = flickr.PhotosetsGetPhotos(linkSelected);

Session["photos"] = photos;

}

else throw new Exception ("link selected is null");


}

else

{

this.flickr = (Flickr)Session["flickr"];

this.photos = (Photoset)Session["photos"];



imageSelected = (string)Session["imageSelected"];

}


foreach (Photo photo in photos.PhotoCollection)

{

ImageButton image = new ImageButton();

CheckBox chk = new CheckBox();

HtmlGenericControl span = new HtmlGenericControl("span");


form1.Controls.Add(span);

span.Controls.Add(image);

span.Controls.Add(chk);



image.ID = photo.PhotoId;

chk.ID = "chk_"+photo.PhotoId;

chk.CheckedChanged += new EventHandler(checkBox_checkChanged);



image.Command += new CommandEventHandler(image_Click);

image.CommandName = photo.PhotoId;

image.ImageUrl = photo.SquareThumbnailUrl;

}






}

protected void image_Click(object sender, CommandEventArgs e)

{


foreach (Photo p in photos.PhotoCollection)

{

if (p.PhotoId == e.CommandName)

{

((Image)sender).ImageUrl = p.SmallUrl;

}

}


Session["imageSelected"] = ((Image)sender).ID;

Trace.Write("selected image: "+ (string)Session["imageSelected"]);

}


message
When you "make a change to the code", e.g. modify any file in the
application
root, your application will restart. Session, Cache and Application
state
all
go away at that point.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


:

I have an odd problem which has me stumped around using Session state.

I have an entirely dynamic page, with all controls added within the
codebehind file. I save various objects to session state so that I can
recreate the page on postback. This all works fine on second running,
but
on
first run after making a change to the code, I get a Null
ReferenceException
on a variable set from the session using e.g. imageSelected =
(string)Session["imageSelected"];

If I select restart and take exactly the same steps, then the
reference
is
set correctly and all is well.

Is there any simple explanation? or is more information required?

Thanks,
Richard
 
R

RichB

OK, thanks Peter I'll try that.

Peter Bromberg said:
You get unusual behaving with various kinds of things like this when
running
in debug mode inside the IDE. Run your code in release mode on a real web
site without Visual Studio involved. If you need to find out whether it is
still happening, write some simple logging code that you can easily turn
on
or off via a boolean appSettings element.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


RichB said:
Sorry, I'm struggling to understand this. I get that the application has
restarted, and that therefore the session state variables in the current
session are lost. However what I do not understand is why when I next try
to
set the variables in the session they do not get set unless I step
through
the code, but if I restart the application again then session variables
are
stored fine.

Sure the session will be lost and the app needs to handle this, but I
just
do not understand why in this particular circumstance the session
variables
are not being set.

I seem to have solved the problem by saving a session variable on the
first
page of the application (which has no need to use Session state). This
just
seems odd to me.....

I'd still like to understand this, and whether I always need to employ
this
work around, or if there is another solution.

Thanks,
Richard


bruce barker said:
if you edit your code, this is a restart.

you need to code for null sessions, as they will happen on your
productions
server. lots of events can cause a restart and session loss. (too much
memory
use, too long idle, iisreset, code drop, etc).


-- bruce (sqlwork.com)


:

That is fine, I am not expecting it to be persisted between restarts,
I
just
expect that when I first run the application and save to Session
state,
that
when I retreive those variables that they will be there without having
to
stop and restart.

However having looked at it further, it just gets more strange. I have
therefore posted my code below. Ihave set a breakpoint at the line: if
(!IsPostback). If after adding a comment in this file I run the code
and
at
the breakpoint select continue and then select an image to cause a
postback
and on hitting the breakpoint select continue again, I get the
NullReferenceException at the line: foreach (Photo photo in
photos.PhotoCollection). However if instead of selecting continue in
the
debugger, I step through the code the Session variables get set and
can
then
be retreived with no problem atall. I am certain that this is
happening,
I
added a comment, steped through and it worked Ok, then removed the
comment
and selected to continue and received the error.

Thanks, Richard
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using FlickrNet;

using System.IO;

using System.Net;

using System.Collections.Generic;





namespace FlickrFriends

{

public partial class SetImages : System.Web.UI.Page

{

Flickr flickr;

Photoset photos;

string imageSelected;

string linkSelected;

List<Photo> downPhotos = new List<Photo>();

protected void Page_Load(object sender, EventArgs e)

{


if (!IsPostBack)

{

flickr = PreviousPage.flickr;

Session["flickr"] = flickr ;


this.linkSelected = PreviousPage.linkSelected;

if (linkSelected != null)

{

photos = flickr.PhotosetsGetPhotos(linkSelected);

Session["photos"] = photos;

}

else throw new Exception ("link selected is null");


}

else

{

this.flickr = (Flickr)Session["flickr"];

this.photos = (Photoset)Session["photos"];



imageSelected = (string)Session["imageSelected"];

}


foreach (Photo photo in photos.PhotoCollection)

{

ImageButton image = new ImageButton();

CheckBox chk = new CheckBox();

HtmlGenericControl span = new HtmlGenericControl("span");


form1.Controls.Add(span);

span.Controls.Add(image);

span.Controls.Add(chk);



image.ID = photo.PhotoId;

chk.ID = "chk_"+photo.PhotoId;

chk.CheckedChanged += new EventHandler(checkBox_checkChanged);



image.Command += new CommandEventHandler(image_Click);

image.CommandName = photo.PhotoId;

image.ImageUrl = photo.SquareThumbnailUrl;

}






}

protected void image_Click(object sender, CommandEventArgs e)

{


foreach (Photo p in photos.PhotoCollection)

{

if (p.PhotoId == e.CommandName)

{

((Image)sender).ImageUrl = p.SmallUrl;

}

}


Session["imageSelected"] = ((Image)sender).ID;

Trace.Write("selected image: "+ (string)Session["imageSelected"]);

}


message
When you "make a change to the code", e.g. modify any file in the
application
root, your application will restart. Session, Cache and Application
state
all
go away at that point.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


:

I have an odd problem which has me stumped around using Session
state.

I have an entirely dynamic page, with all controls added within the
codebehind file. I save various objects to session state so that I
can
recreate the page on postback. This all works fine on second
running,
but
on
first run after making a change to the code, I get a Null
ReferenceException
on a variable set from the session using e.g. imageSelected =
(string)Session["imageSelected"];

If I select restart and take exactly the same steps, then the
reference
is
set correctly and all is well.

Is there any simple explanation? or is more information required?

Thanks,
Richard
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top