By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,247 Members | 871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,247 IT Pros & Developers. It's quick & easy.

Holding a class in the Session

P: n/a
I have the following code:

Session["CurrentUser"] = new CurrentUser("TEST");

When I postback to the server, the Session["CurrentUser"] is null.
My guess is a only the refence to my actual class is stored, rather than
the class.
Looking on the internet, one solution posted was populating
Session["CurrentUser"] in
Session_Start in a global.asax file. Again, on postback, the value is
still null.

Can anyone help me out with this problem?

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
All reference types (such as classes) are stored in memory as references.
Session State is memory. In other words, when you store an instance of a
class in Session State is not important. It will be stored in Session State
when you store it.

There are a couple of possibilities:

1. An exception occurs during the instantiation of the class, which results
in nothing being stored in Session. Have you checked to see whether it is
there immediately after you store it?

2. The application is restarting between the time you add the instance to
the Session and the time you re-read it from the Session. This can happen
when, for example, you are debugging and rebuilding your app when you start
the debugger.

3. Something is removing the instance from Session before you try to access
it.

4. Check your spelling. Is the key you use to insert it exactly the same as
the key you use to fetch it?

Other than that, without knowing anything else about your app, I can't
guess.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Steven Blair" <st**********@btinternet.com> wrote in message
news:un****************@TK2MSFTNGP14.phx.gbl...
I have the following code:

Session["CurrentUser"] = new CurrentUser("TEST");

When I postback to the server, the Session["CurrentUser"] is null.
My guess is a only the refence to my actual class is stored, rather than
the class.
Looking on the internet, one solution posted was populating
Session["CurrentUser"] in
Session_Start in a global.asax file. Again, on postback, the value is
still null.

Can anyone help me out with this problem?

Steven

*** Sent via Developersdex http://www.developersdex.com ***

Mar 16 '06 #2

P: n/a
Thanks for the reply.

The spelling is correct. I create the object, populate one of the member
variables, then on next postback that object is null.
Here is my code:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["CurrentUser"] = new CurrentUser("Test");
}

CurrentUser user = (CurrentUser)Session ["CurrentUser"];
//Just to test
}
Thats all I have, and can't understand why its being removed.
*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '06 #3

P: n/a
Kevin,

Session state is not always a memory. It could be a DB or Remove machine. In
these cases the class needs to be serializable in order to survive
postbacks. By default, though the session state is kept in the worker
process memory, serializable or not it should stay there.

Steven, Is it possible that your postback comes with a big delay. For
example you hit the site for the first time and add the session state and
then you post back after 20 min let say when the session has expired.

Also the server uses cookies to track the session state make sure that the
cookies are not blocked in your borwser.
--
HTH
Stoitcho Goutsev (100)
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uB**************@TK2MSFTNGP14.phx.gbl...
All reference types (such as classes) are stored in memory as references.
Session State is memory. In other words, when you store an instance of a
class in Session State is not important. It will be stored in Session
State when you store it.

There are a couple of possibilities:

1. An exception occurs during the instantiation of the class, which
results in nothing being stored in Session. Have you checked to see
whether it is there immediately after you store it?

2. The application is restarting between the time you add the instance to
the Session and the time you re-read it from the Session. This can happen
when, for example, you are debugging and rebuilding your app when you
start the debugger.

3. Something is removing the instance from Session before you try to
access it.

4. Check your spelling. Is the key you use to insert it exactly the same
as the key you use to fetch it?

Other than that, without knowing anything else about your app, I can't
guess.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Steven Blair" <st**********@btinternet.com> wrote in message
news:un****************@TK2MSFTNGP14.phx.gbl...
I have the following code:

Session["CurrentUser"] = new CurrentUser("TEST");

When I postback to the server, the Session["CurrentUser"] is null.
My guess is a only the refence to my actual class is stored, rather than
the class.
Looking on the internet, one solution posted was populating
Session["CurrentUser"] in
Session_Start in a global.asax file. Again, on postback, the value is
still null.

Can anyone help me out with this problem?

Steven

*** Sent via Developersdex http://www.developersdex.com ***


Mar 16 '06 #4

P: n/a
> Session state is not always a memory. It could be a DB or Remove machine.
In these cases the class needs to be serializable in order to survive
postbacks.
That's right. I didn't mention that. I assumed (bad idea) that, since he was
not very experienced, he would be working with the default Session State.
Thanks for pointing that out.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl... Kevin,

Session state is not always a memory. It could be a DB or Remove machine.
In these cases the class needs to be serializable in order to survive
postbacks. By default, though the session state is kept in the worker
process memory, serializable or not it should stay there.

Steven, Is it possible that your postback comes with a big delay. For
example you hit the site for the first time and add the session state and
then you post back after 20 min let say when the session has expired.

Also the server uses cookies to track the session state make sure that the
cookies are not blocked in your borwser.
--
HTH
Stoitcho Goutsev (100)
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uB**************@TK2MSFTNGP14.phx.gbl...
All reference types (such as classes) are stored in memory as references.
Session State is memory. In other words, when you store an instance of a
class in Session State is not important. It will be stored in Session
State when you store it.

There are a couple of possibilities:

1. An exception occurs during the instantiation of the class, which
results in nothing being stored in Session. Have you checked to see
whether it is there immediately after you store it?

2. The application is restarting between the time you add the instance to
the Session and the time you re-read it from the Session. This can happen
when, for example, you are debugging and rebuilding your app when you
start the debugger.

3. Something is removing the instance from Session before you try to
access it.

4. Check your spelling. Is the key you use to insert it exactly the same
as the key you use to fetch it?

Other than that, without knowing anything else about your app, I can't
guess.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Steven Blair" <st**********@btinternet.com> wrote in message
news:un****************@TK2MSFTNGP14.phx.gbl...
I have the following code:

Session["CurrentUser"] = new CurrentUser("TEST");

When I postback to the server, the Session["CurrentUser"] is null.
My guess is a only the refence to my actual class is stored, rather than
the class.
Looking on the internet, one solution posted was populating
Session["CurrentUser"] in
Session_Start in a global.asax file. Again, on postback, the value is
still null.

Can anyone help me out with this problem?

Steven

*** Sent via Developersdex http://www.developersdex.com ***



Mar 16 '06 #5

P: n/a
Just thought I would finish this thread since I worked out the problem.
Thanks for the help.

The application was creating the CurrentUser class BEFORE I had logged
in, so the newly created class would have been sitting in Session x, but
after I was successfully authenticated, I would then be looking at
Session y.
*** Sent via Developersdex http://www.developersdex.com ***
Mar 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.