Storing a object in session object

Results 1 to 7 of 7

Thread: Storing a object in session object

  1. #1
    Finnur Guest

    Default Storing a object in session object

    I&#039m working on a online-shopping project and I&#039m wondering about the pro and cons with storing information in a object stored in the session object. What I had in mind was storing the bought items in that object as the user wanders around the site and buys things. Currently this information stored in cookies and the process is a littlebit jerky (strings). <BR>My question is: how much will I gain by using session to store a object or loose in memory resources at the server? Shall I keep on with the cookies or change to session object?

  2. #2
    Join Date
    Dec 1969
    Posts
    2,849

    Default RE: Storing a object in session object

    You&#039re asking for nothing but touble if you use session variables to store objects. (If you want to use session variables, use them to store those strings you&#039re using cookies to store now.) Storing objects in session variables is evil. Here are a couple of articles on why:<BR><BR>http://www.activeserverpages.com/learn/nosessionobjects.asp<BR>http://www.4guysfromrolla.com/webtech/092098-2.shtml<BR><BR>Happy Programming!

  3. #3
    Ethan Kaplan Guest

    Default RE: Storing a object in session object

    Does this apply even when you kill the object in the Session variable after it is used?<BR><BR>Ethan

  4. #4
    G. Antikas Guest

    Default RE: Storing a object in session object

    So where can the poor programmer store his recordset object? For example i need to display the results of a query in sets of 10... how do you suggest i do that? I cant find a place to store the entire recordset...


  5. #5
    Join Date
    Dec 1969
    Posts
    68

    Default RE: Storing a object in session object

    Serialize the object into and out of session variables, and then create and destroy the object at the page level.<BR><BR>Minimal example:<BR>Let&#039s say I had an object that counted the number of pages a given user visited. The object has one method - AddToHitCount - and one property - HitCount. This&#039d take about a minute to build in VB.<BR><BR>I might be tempted to do the following. In global.asa, I&#039d have a Session_OnStart that did something like <BR><BR>Session("ObjHitCounter") = Server.CreateObject("MyLib.HitCounter")<BR><BR>At the top of each page, I&#039d say <BR><BR>Session("ObjHitCounter").AddToHitCount<BR> <BR>And back in global.asa, in Session_OnEnd, I might write Session("ObjHitCounter").HitCount to a log file or a database or something. You get the idea.<BR><BR>We both agree that what I just described is a really bad idea.<BR><BR>Instead, at the top of each page, declare a local variable:<BR><BR>Dim obj_hitcounter<BR>obj_hitcounter = Server.CreateObject("MyLib.HitCounter")<BR><BR>The n read the object&#039s state from a session variable.<BR><BR>obj_hitcounter.HitCount = Session("HitCount")<BR><BR>Up the hit count.<BR><BR>obj_hitcounter.AddToHitCount<BR><BR> Put the values back into Session variables.<BR><BR>Session("HitCount") = obj_hitcounter.HitCount<BR><BR>Clean up the object.<BR><BR>Set obj_hitcounter = Nothing<BR><BR>Clearly, this is a lame example. But using objects to do the work of a web site is not a lame idea, and per-user objects are intuitive. It&#039s just a shame that the existing framework does not support them directly. And that you have to explicitly null references in a garbage collected language. And...<BR>

  6. #6
    Jon Vandermeulen Guest

    Default Paging recordsets.

    &#039Set how many records per page we want<BR>NumPerPage = 15<BR><BR>&#039Retrieve what page we&#039re currently on<BR>If Request.QueryString("CurPage") = "" then<BR>CurPage = 1 &#039We&#039re on the first page<BR>Else<BR>CurPage = Int(Request.QueryString("CurPage"))<BR>End If<BR><BR>Set conn = Server.CreateObject("ADODB.Connection")<BR>Set rs = Server.CreateObject("ADODB.Recordset")<BR>conn.Ope n DBCONN<BR>rs.CacheSize = NumPerPage<BR>SQL="SELECT SOMEDATA.* FROM SOMEDATA WHERE (SOMEFIELD = &#039SOMEVALUE&#039)"<BR>rs.Open SQL, conn, 1, 3<BR>rs.MoveFirst<BR>rs.PageSize = NumPerPage<BR>&#039Get the max number of pages<BR>TotalPages = rs.PageCount<BR>&#039Set the absolute page<BR>rs.AbsolutePage = CurPage<BR>%&#062;<BR><BR>Then somewhere on the page, you can provide links to the next page:<BR>&#060;A HREF="thispage.asp?curpage=&#060;%= curpage + 1 %&#062;Next&#060;/A&#062;<BR><BR>and go back one page:<BR>&#060;A HREF="thispage.asp?curpage=&#060;%= curpage - 1 %&#062;Back&#060;/A&#062;<BR><BR>This is far less evil than storing an entire recordset in sessions.

  7. #7
    Renee G. Guest

    Default RE: Paging recordsets.

    It appears that by using the cachesize property that you are limiting the size of the recordset brought back from the database? I am still puzzled as to what portion in this code tells the next (or previous) page exactly what portion of the sql query to bring back!<BR><BR>Also when and where is the request.querystring created, and when do I explicitly close the request object(do I need to?). When would I close the recordset and set it to nothing?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •