By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

Session Variables - HELP !!

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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("UserSessio n") is
Nothing Then _
System.Web.HttpContext.Current.Session("UserSessio n")=new
UserSession()
Return
Ctype(System.Web.HttpContext.Current.Session("User Session"), 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.

"Stuart" <St****@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
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

Nov 19 '05 #2

P: n/a
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...
"David Jessee" wrote:
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("UserSessio n") is
Nothing Then _
System.Web.HttpContext.Current.Session("UserSessio n")=new
UserSession()
Return
Ctype(System.Web.HttpContext.Current.Session("User Session"), 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.

"Stuart" <St****@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
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


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.