lifetime of Session object

Discussion in 'ASP .Net' started by Colin Basterfield, Jun 2, 2004.

  1. Hi,

    I am confused...

    I have the following code in my Page_Load method
    private void Page_Load(object sender, System.EventArgs e)

    {

    // Put user code to initialize the page here

    if (!IsPostBack)

    {

    SiteSummary siteSummary = new SiteSummary();

    RevenueList revList = siteSummary.GetRevenueList();

    Session["RevenueList"] = revList;

    BindGrid();

    }

    }

    Now my understanding is that I could , and have been loading the Revenue
    list directly into the Session object, and then in the BindGrid() method I
    assign the collection to the datalist datasource thus:

    private void BindGrid()

    {

    dlRevenueItems.DataSource = (RevenueList)Session["RevenueList"];

    dlRevenueItems.DataBind();

    }

    Now this was working yesterday, but now all of a sudden, when I run it the
    assignment to the datasource fails, saying that it isn't set to the instance
    of an object.

    I am wholly confused when something works intermittently when there has been
    no code change.

    I can't declare the list private to the overall page class as it falls out
    of scope the minute the BindGrid is called, hence the change to the Session
    object.

    Confused, probably out of ignorance

    TIA

    Colin B
    Colin Basterfield, Jun 2, 2004
    #1
    1. Advertising

  2. well, you are chancing by binding directly out of session since the runtime
    makes no guarantee that the objects in session will be around when you use
    it. I believe your real question is why is the data not there?
    You need to examine the session id before you go any further. put some code
    to display the session.id on each page load. if the session id changes, your
    session variables will no longer be present since the session end event
    would have been triggered.

    here is some background:
    http://support.microsoft.com/default.aspx?scid=kb;en-us;555082
    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Colin Basterfield" <> wrote in message
    news:...
    > Hi,
    >
    > I am confused...
    >
    > I have the following code in my Page_Load method
    > private void Page_Load(object sender, System.EventArgs e)
    >
    > {
    >
    > // Put user code to initialize the page here
    >
    > if (!IsPostBack)
    >
    > {
    >
    > SiteSummary siteSummary = new SiteSummary();
    >
    > RevenueList revList = siteSummary.GetRevenueList();
    >
    > Session["RevenueList"] = revList;
    >
    > BindGrid();
    >
    > }
    >
    > }
    >
    > Now my understanding is that I could , and have been loading the Revenue
    > list directly into the Session object, and then in the BindGrid() method I
    > assign the collection to the datalist datasource thus:
    >
    > private void BindGrid()
    >
    > {
    >
    > dlRevenueItems.DataSource = (RevenueList)Session["RevenueList"];
    >
    > dlRevenueItems.DataBind();
    >
    > }
    >
    > Now this was working yesterday, but now all of a sudden, when I run it the
    > assignment to the datasource fails, saying that it isn't set to the
    > instance
    > of an object.
    >
    > I am wholly confused when something works intermittently when there has
    > been
    > no code change.
    >
    > I can't declare the list private to the overall page class as it falls out
    > of scope the minute the BindGrid is called, hence the change to the
    > Session
    > object.
    >
    > Confused, probably out of ignorance
    >
    > TIA
    >
    > Colin B
    >
    >
    >
    >
    Alvin Bruney [MVP], Jun 3, 2004
    #2
    1. Advertising

  3. Hi there,

    many thanks for this, I will do some checking, both in the code and the
    link, however I did do some checks by breaking at the point where it tries
    to load the RevList from the Session object, and using a quick watch I could
    see that the data was in fact there, but when I stepped it on from that
    point, that is when it goes boom!

    Anyways thanks again.
    Colin

    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:...
    > well, you are chancing by binding directly out of session since the

    runtime
    > makes no guarantee that the objects in session will be around when you use
    > it. I believe your real question is why is the data not there?
    > You need to examine the session id before you go any further. put some

    code
    > to display the session.id on each page load. if the session id changes,

    your
    > session variables will no longer be present since the session end event
    > would have been triggered.
    >
    > here is some background:
    > http://support.microsoft.com/default.aspx?scid=kb;en-us;555082
    > --
    > Regards,
    > Alvin Bruney
    > [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > Got tidbits? Get it here... http://tinyurl.com/27cok
    > "Colin Basterfield" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I am confused...
    > >
    > > I have the following code in my Page_Load method
    > > private void Page_Load(object sender, System.EventArgs e)
    > >
    > > {
    > >
    > > // Put user code to initialize the page here
    > >
    > > if (!IsPostBack)
    > >
    > > {
    > >
    > > SiteSummary siteSummary = new SiteSummary();
    > >
    > > RevenueList revList = siteSummary.GetRevenueList();
    > >
    > > Session["RevenueList"] = revList;
    > >
    > > BindGrid();
    > >
    > > }
    > >
    > > }
    > >
    > > Now my understanding is that I could , and have been loading the Revenue
    > > list directly into the Session object, and then in the BindGrid() method

    I
    > > assign the collection to the datalist datasource thus:
    > >
    > > private void BindGrid()
    > >
    > > {
    > >
    > > dlRevenueItems.DataSource = (RevenueList)Session["RevenueList"];
    > >
    > > dlRevenueItems.DataBind();
    > >
    > > }
    > >
    > > Now this was working yesterday, but now all of a sudden, when I run it

    the
    > > assignment to the datasource fails, saying that it isn't set to the
    > > instance
    > > of an object.
    > >
    > > I am wholly confused when something works intermittently when there has
    > > been
    > > no code change.
    > >
    > > I can't declare the list private to the overall page class as it falls

    out
    > > of scope the minute the BindGrid is called, hence the change to the
    > > Session
    > > object.
    > >
    > > Confused, probably out of ignorance
    > >
    > > TIA
    > >
    > > Colin B
    > >
    > >
    > >
    > >

    >
    >
    Colin Basterfield, Jun 3, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    416
  2. =?Utf-8?B?RGlmZmlkZW50?=

    Worker process and Cache object's lifetime

    =?Utf-8?B?RGlmZmlkZW50?=, Feb 2, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    463
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 2, 2006
  3. pt
    Replies:
    8
    Views:
    771
  4. Oliver Tengler

    SWIG: Tie lifetime of object to a result

    Oliver Tengler, Nov 14, 2003, in forum: Python
    Replies:
    0
    Views:
    275
    Oliver Tengler
    Nov 14, 2003
  5. Chris
    Replies:
    1
    Views:
    387
    Nick Gilbert
    Jun 4, 2010
Loading...

Share This Page