Re: Session State null at compile

Tech-Archive recommends: Fix windows errors by optimizing your registry



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 [C# MVP]" <pbromberg@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:87937AC3-5382-4B01-9204-376809F9321A@xxxxxxxxxxxxxxxx
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" wrote:

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





.


Quantcast