473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Holding a class in the Session

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
5 1954
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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: | last post by:
Hi, I want to do things this way: I have a bunch of stuff that I want to keep track of while a user is connected to the site. Maybe 50 little peices of information. So I know I can make 50...
2
by: Gizmo | last post by:
hi all i have a bit of a prob with some 2 classes iv created if say i have class A and class B. class A hold an atribute of class B and class be Holds an Atribute of class A. both are...
3
by: Grant | last post by:
We have over 130 new users that signed on within 3 weeks. Once in a great while we get email from some user who cannot create account successfully. Here is the scenario New user will...
1
by: Chris Austin | last post by:
A co-worker asked me today what would happen if Session.Add() method was called multiple times with the same key. My first thought was that it would throw some sort of duplicate key exception. ...
2
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 ...
2
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in...
15
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with 10 items it might use 1KB, and if I load it with...
4
by: Andy Kasotia | last post by:
I have read couple of articles warning against the use of storing VB COM objects (Apartment Threading) in Session Variables due to the fact that these variables could go bad. My question is...
8
by: shlomisderot | last post by:
Hello all, I'm not familiar with PHP session, my question is if session save the values when I brows to another page. It is possible to keep the session values when I go to other page (in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.