Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 10th, 2004, 10:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default Storing objects in Session variables

My teacher told me something I think is a bit dodgy; maybe some of you can clarify this:
If you want to use an object with each new user, then you’ll have to store each complete object in a Session variable using the global.asax.
The reason this sound weird to me is that in classic ASP it was terrible idea to store objects in sessions. I know that in C# everything is an object, so one way or another you’ll have to save an object in a session at some point. But an entire object? If a user visits my website then each object in my code behind is a unique object, right? Why would I need to store it in a session?

 - mega
Moving to C# .NET
__________________
 - mega
Aspiring JavaScript Ninja
  #2 (permalink)  
Old November 11th, 2004, 08:33 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello,
Quote:
quote:Originally posted by mega
  Why would I need to store it in a session?
A browser hits a site. The site creates a unique identifier (ID), allocates some memory on the server (the session), and tags this memory with this ID. The server places this ID in the response header, and this "cookie" is passed back to the client browser. This cookie is timestamped (it will time out in 20 minutes.)

All subsequent requests from the browser to the server contain this ID. The server uses this ID and tries to find the block of memory it belongs to. It find the memory. Hence, the session is still alive, the cookie is given a fresh timestamp (a new 20 minute timeout), and sent back to the browser with the rest of the page.

Browsers that are spawned from this browser share the cookie (they pass the same ID back and forth to the server.) Every request resets the 20 minute timer.

If you go to lunch for 60 minutes, come back, and hit the site, your ID cannot be found by the server, so it creates a new session (and you handle this and make the use login again, etc...) Your session may have expired on the server - and in the simpleist of environments, fired a SessionEnd event, whereby you could have logged this into a database. But, this is the server side, so it has no idea what the client side is or isn't doing.
(Thanks to us7892)
Quote:
quote:Originally posted by mega
  If a user visits my website then each object in my code behind is a unique object, right?
yes,
Each page is executed separately, within its own context. Two page executions happening simultaneously are not related in any way apart from their existance within the same application. Only during the execution of one page, in one page's context will the same "current" value in a class' public static variable be visible.

Let me say that you have this:

Public Class MyClass
   Public Static MyStaticVariable As Integer = 0
End Class

User A:
MyClass.MyStaticVariable=1
Response.Write(MyClass.MyStaticVariable) 'This prints "1"

User B:
Response.Write(MyClass.MyStaticVariable) 'This prints "0"
(Thanks to Peter Lanoie)

hope I could make you understood the difference between sessions and global objects.

--------------------------------------------
Mehdi.:)
  #3 (permalink)  
Old November 11th, 2004, 09:55 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

mega-

The rule of thumb I try to follow is this:

Store as little information as possible in the session. If you have an object with 4 strings as properties, store the 4 strings to the session, not the object. Then restore the object properties from those 4 strings. This will result in a smaller footprint in session and therefore be more efficient.

Of course as always in programming, there are exceptions. If the object you are dealing with is very large and complicated, it may simply not be practical to store it's individual parts separately in session. In this case you can just save the whole object. You must keep in mind, however, that you'll be chewing up memory faster. Once you are done with the object, you need to manually destroy its instance in the session because the garbage collection process doesn't deal with that. Only when the session expires on the server will the contents of the session be destroyed.
  #4 (permalink)  
Old November 12th, 2004, 05:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Thanks for the replies. I guess my teacher was wrong then (great teacher :-s)..
Won’t the garbage collector finalize the object when the session dies, planoie?


 - mega
Moving to C# .NET
  #5 (permalink)  
Old November 12th, 2004, 10:03 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well yes, when the session is destroyed, the object contained in it will get destroyed. But as long as the session is alive that object is alive so you need to manually clean it up if you are done with it so that you don't use up too much memory.
  #6 (permalink)  
Old November 12th, 2004, 01:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Why would I put an object in a session, if I don't use it all the time the user is using my site? I got no way of knowing if a user has closed the window or is away drinking coffee for 10 minutes. I'm just asking this because I know little of ASP.NET and haven't seen any "is-user-still-watching-my-page" control or method.
:)

 - mega
Moving to C# .NET
  #7 (permalink)  
Old November 13th, 2004, 02:08 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you cant get whether a user is still watching your site or not exactly...
just sessions give this chance to you in a time period(not that exact time the user leaves your site) ...
suppose the time period is 20 minutes ...now our user hits the site so an unique ID(or session)is allocated for this user if in this 20 minutes our user doesnt send any request to server the session with that special ID will die otherwise it receives a fresh timestamp(a new 20 minute timeout) ...
(maybe the user is still watching your site but because he hasnt sent any request to server in that 20 minutes its session would die and in next request another new session(with new ID) would be created so one user could cause creation of several sessions).
Quote:
quote:Originally posted by mega
 I got no way of knowing if a user has closed the window or ...
you cant get the time your user closes the window with help of sessions(server-side controls).you should check it with client-side facilities,for example take a look at this thread.

--------------------------------------------
Mehdi.:)
  #8 (permalink)  
Old November 14th, 2004, 04:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

You're right, it's just like ol' ASP. This article give a better solution than the thread: http://www.4guysfromrolla.com/webtech/100604-1.shtml
Just can't find the window.onbeforeunload in my Rhino book, but it works in Firefox.. Are there a new JS release i'm not aware of??

 - mega
Moving to C# .NET
  #9 (permalink)  
Old November 16th, 2004, 02:38 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

mega,it's better to ask this question in JavaScript forum.

--------------------------------------------
Mehdi.:)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
storing field values to variables imaginevictor SQL Server 2000 1 July 3rd, 2007 02:17 AM
Storing a dataset across a session jainnid General .NET 1 December 2nd, 2005 03:17 PM
Storing binary data in $_SESSION variables richard.york Pro PHP 0 August 30th, 2003 05:23 PM



All times are GMT -4. The time now is 05:18 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.