473,383 Members | 1,829 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,383 software developers and data experts.

Accessing a Persistent Object throughout a user session

I want to create an object which is attached to the specific user session and
I want to be able to access that object directly throughout the Pages, Page
Controls, and Master Pages of the site.

I'm assuming that the best way to do that is to create the object and then
put it into the Session? If so, a few questions about the best way to do that:

1. If I want to reliably access that object on any and every page, how do I
make sure that the object is already in the Session for that user? Do I have
to put logic EVERYWHERE that it's used that says, 'if(Session["MyObj"]==null)
Session["MyObj"] = new MyObj()'?

2. When I access the object from the Session, do I have to 'cast' the object
before I can use it on the pages (e.g. '(MyObj)Session["MyObj"]')?

3. Is there some better way to make the object available throughout the
Session without actually inserting it manually INTO the Session and then
extracting it on every page where it's used?

Alex
Apr 10 '06 #1
3 1941
Alex,
see inline:

"Alex Maghen" wrote:
I want to create an object which is attached to the specific user session and
I want to be able to access that object directly throughout the Pages, Page
Controls, and Master Pages of the site.

I'm assuming that the best way to do that is to create the object and then
put it into the Session? If so, a few questions about the best way to do that:
-- Yes, store the object in Session with a descriptive key.
1. If I want to reliably access that object on any and every page, how do I
make sure that the object is already in the Session for that user? Do I have
to put logic EVERYWHERE that it's used that says, 'if(Session["MyObj"]==null)
Session["MyObj"] = new MyObj()'?
-- You could create the session object one time in the Session_Start
global.asax handler. Note that you must actually put something into Session
for a user in order to get Session_Start to fire.

2. When I access the object from the Session, do I have to 'cast' the object
before I can use it on the pages (e.g. '(MyObj)Session["MyObj"]')?
Yes: MyObj obj = (MyObj)Session["MyObj"];
3. Is there some better way to make the object available throughout the
Session without actually inserting it manually INTO the Session and then
extracting it on every page where it's used? -- Covered above.

Good Luck!
Peter
Alex

Apr 10 '06 #2
Hi Alex,

I think Peter's suggestion reasonable. As for SessionState, it is
associated with each client user through a sessionID, by default it is
stored in client user's cookie. And the SessionState colleciton is like a
dictionary collection, it store all the objects or object reference as
object type, so you need to explicitly cast it when retrieve the certain
classs instance from SessionState.

And Peter's suggestion on using Session_Start event to initialize a Session
variable is a good idea. BTW, I still recommend you use the protected code
block such as (if Session[key] == null then Session[key] = new....) when
accessing such object, this is the best practice which can avoid Null
reference exception.

In addition, I'm not sure whether you're using the ASP.NET 2.0 and the
users in your application is authenticated through the forms
authentication. If that's the case, you can also consider using the Profile
service which can provide strong-typed data storage for user specific datas:

#ASP.NET Profile Properties
http://msdn2.microsoft.com/en-us/lib...x3(VS.80).aspx

Please feel free to post here if there is anything else we can help.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 11 '06 #3
Hi Alex,

Have you got any progress on this issue or do you still have any further
questions on this? If anything else we can help, please feel free to post
here.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Apr 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ndsoumah | last post by:
hello guys I'm trying to get access to variables I put in a session variable from another page and it fails... here's the exact situation main file page1.php
0
by: jbi130 | last post by:
Not sure if the subject is a good description of what I'm after... I have a client-server architecture (current client in Java, server in C not that it really matters). I'm wanting to implement...
0
by: obhayes | last post by:
Hi All, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the...
1
by: brad | last post by:
Hi, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the...
8
by: bashful.belle | last post by:
I'm using Forms authentication and a non persistent cookie in my asp.net application. How do i get the cookie to time out after a period of inactivity, say 10 minutes, and force the user to login...
3
by: sanchita | last post by:
Hello everyone, I didn't get any response in "Security" forum hence posting here again. I am having problem with persistent cookies. Even after setting "CreatePersistentCookie" to true in...
5
by: lawpoop | last post by:
Hello all! I'm working on a PHP site using Postgres as a back-end. I have an include at the top of each page that runs the pg_connect function. I'm implementing some temporary tables, which I...
9
by: mel | last post by:
Hi all, I need a persistent TCP connection with my web server over page reloads. This means that, even if the user goes to a different page (in my domain), I want to keep a TCP connection...
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.