Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Variables Timeout - Possible Solution?

Status
Not open for further replies.

Silo4

Programmer
Nov 7, 2002
17
GB
Hi,

I want to store data in session variables available to all pages in my application, after 20 minutes though they are lost in the trash.

I've been playing with a way of retaining session variables beyond session timeout. I was wondering if someone had any thoughts on a possible solution i've come up with?

Here it is...

1) Session vars are saved within the page when it loads and passed back to session vars before leaving.

2)A common property is used to Set and Get each variable.

This following code is added to a pagebase class, inherited by all pages.

Code:
Protected Sub PageBase_Load(ByVal sender As Object, ByVal e As EventArgs)
    'On the first page load save the session vars to viewstate
    If Not IsPostBack Then
        SetVarsToViewState()
    End If
End Sub      

Protected Property Var(ByVal VarName As String) As String
   'Return the var
   Get
       If Session(VarName) = Nothing Then
           'if the session var has been lost
           Return ViewState(VarName)
        Else
           Return Session(VarName)
        End If
    End Get
    'Save the var
    Set(ByVal Value As String)
        Session(VarName) = Value
        ViewState(VarName) = Value
    End Set
End Property
        
Protected Sub SetVarsToSession()
    'Save all session vars to viewstate
    Dim vsEnum As IDictionaryEnumerator = ViewState.GetEnumerator
    Do While vsEnum.MoveNext
         Session(vsEnum.Key) = ViewState(vsEnum.Key)
    Loop
End Sub

Protected Sub SetVarsToViewState()
    'Save all viewstate vars to session
    Dim ssEnum As IEnumerator = Session.GetEnumerator
    Do While ssEnum.MoveNext
         Viewstate(ssEnum.Current) = Session(ssEnum.Current)
    Loop
End Sub

Within the page we can now store and retrieve any variable we want using...

Code:
Dim MyVar As String = Var("Var1")

Var("Var1") = "Hello"

We must save the vars back into session vars before we postback or move to another page, simply by calling...

Code:
    SetVarsToSession()



EnableViewState="false" cannot set at the page level otherwise the viewstate vars are lost on postback.
The code could be changed to use hidden fields as an alternative.

That's it.

It seems to work, but I'd really appreciate any comments you might have.

Thanks

Silo4
 
You can increase the session timeout in the web.config file


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm,

My application does a lot of work client side and only saves to database at the end of a specific task, which could take an hour or so to complete.

I want users to be able to leave the PC and come back to it without my application loosing their data and them having to log back in.

Cheers though.

Silo4
 
Won't a larger session timeout help with that?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It would, but where do you draw the line. I could set it to 12 hours, but then i'm not clearing the server memory and enough users could cause out of memory issues

Silo4
 
I guess you have to have a look at what your longest process will take. If it will take 1 hour, and your server will cope with the memory aspect of that, then increasing the timeout may suffice.

If you need to use more persistent method, you could use any of the following:

1) Database
2) ViewState
3) Cookies

#1 is the most secure and persistent method but does involve more round trips to the server and back.

#2 is what you've already attempted and can work well but what happens if the user closes their browser?

#3 is possibly a good choice but it is limited in size and the user doesn't have to accept the cookie.

I don't think we can decide which is the best method for your scenario, but hopefully this will help you make your decision.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for all you advice on this.

I've just discovered that my code doesn't work when you hit refresh in the browser!! When you do so, it doesn't save the viewstate information. Presumably this is by design, but it has scuppered my plans.

I think saving the Session vars in the database is going to be my best option. It'll be slower but i don't think I've much choice. I don't want to rely on the user having to turn cookies on.

Cheers again.

Silo4
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top