473,587 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1951
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
3962
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
1220
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 a web client with Python and need to do more than just connect and work with the database behind the server. I've already implemented a client...
0
2481
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 webserver (therefore do not intend to use the session object- as you cannot gaurantee which server the user will go to). I want to store a small value...
1
4598
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 webserver (therefore do not intend to use the session object- as you cannot gaurantee which server the user will go to). I want to store a small value...
8
3178
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 again? If I close the browser, the next time I use the application, it makes me log in - and That's fine. However if I keep the browser session...
3
4287
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 "FormsAuthentication.SetAuthCookie" I'm being logged out after the specifed timeout provided in "forms" element of web.config. I read somewhere that...
5
2757
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 understand are destroyed automatically at the end of the session. It seems to me that when I navigate to a new page, or re-load my existing page, I...
9
2670
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 live. I know I can do something similar with globalStorage in FF, window.name & userData on IE, but those storages only hold strings.
8
10813
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 steps successfully with the following code... Control myContainer = (Control)Wizard1.FindControl("SideBarContainer"); DataList mySideBarList =...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.