Session Variables - HELP !!

G

Guest

Hi there

I am using several processes within an .asp application that store variables
in to session - typically:

Session("UniqueName") = Value

I am having a hell of a time overwriting the Session value once it has been
stored the first time - namely I can't

I am even following a code example in a book which has a function that
creates a dataset and puts it in Session:

dim ds as dataset.....etc. etc....

Session("Dataset") = ds

A process will then retrieve that dataset and add a row and store the
ammended dataset in to the same Session("Dataset")

Another process then needs to retrieve the ammended DataSet but it is always
the original dataset retrieved and the ammendments are missing ! (This is not
the only occurrance of this problem in my application) - I must be missing
something here - PLEASE HELP ME

I appreciate any input you may have - in the above example if I store the
ammended dataset as Session("Dataset2") the whole process works fine, so I am
confident the code is good - for unavoidable reasons I can not change the
stored dataset name in my working app so this is not a solution for me - and
surely this should work anyway ???

(I have tried Session.Add("Dataset", ds) as an alternative to no avail)

Thanks
 
D

David Jessee

This should fix it, and clean up your session management too. Sit down and
define exactly what the user's session needs to look like for the
application and create a class that represents that information. Then allow
that class to manage its own lifecycle. Here's an Example....

<Serializable()> Public Class UserSession
Private _shoppingCart as Dataset
Private _birthday as DateTime

Public Property ShoppingCart as Dataset
Get
Return _shoppingCart
Set(value as Dataset)
_shoppingCart = value
End set
End Property

Public Property Birthday() as DateTime
Get
Return _birthday
End Get
Set(Value as DateTime)
_birthday = value
End Set
End Property

'This shared property manages the class's lifecycle for you
Public Shared Readonly Property Current() as UserSession
Get
If System.Web.HttpContext.Current is Nothing is Nothing Then
Return Nothing
If System.Web.HttpContext.Current.Session("UserSession") is
Nothing Then _
System.Web.HttpContext.Current.Session("UserSession")=new
UserSession()
Return
Ctype(System.Web.HttpContext.Current.Session("UserSession"), UserSession)
End Get
End Property
End Class

If you need to interact with your session, Just refer to UserSession.Current

e.g.
txtBirthday.Text = UserSession.Current.Birthday.ToShortDateString
dgCart.DataSource = UserSession.Current.ShoppingCart
UserSession.Current.Birthday = DateTime.Now

This way:
1) Your Session Class Manages its own lifecycle
2) You have a firm image of what your session state, as a whole looks like.
You can avoid sporadic Session variables floating around, bloated session
state, and you don't have to worry about the typos inherent in the
session("StringKeyName") construct.

Hope this helps you out.
 
G

Guest

Thanks David - this is a TOP recommendation which I have implemented and
indeed it makes life and code far more straight forward with Session
variables - so thanks very much...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top