views:

54

answers:

4

If i declared a variable as public in a web page, does this variable can hold the same value between different sessions, or each session has its own copy?

And if yes it holds between sessions, how to prevent that? is there an attribute to declare as for one session copy?

May be its a silly question? but i am confused about something..

+4  A: 

The variable is only available to the single request unless you store it in the Session or Cache. If it is public it will be available to other objects created during the request, but only to those object associated with the request. If you declare it to be static as well, then it will be available for the lifetime of the application -- but this sounds like what you don't want, so simply avoid doing it.

tvanfosson
so if its static it will keep holding the same value between different sessions?
Amr ElGarhy
Yes, if its static, it will hold the same value for the entire app lifecycle. But why would you want to declare static variables on a Page?
SharePoint Newbie
I don't want in the page its self, but may be in a class which the page call
Amr ElGarhy
If you want the same value to be retained accross sessions: <br />1. Put it in some static/singleton. Putting it in the page is a bad idea. <br />2. Putting it in a singleton/static is bad as it would die with the process, so use the DB.
SharePoint Newbie
I don't want to keep it, for example, if i declared the sqlconnection as static, i want to know if all will access the same instance or not and when it will die?
Amr ElGarhy
If you want data to persist between sessions for a single user, you should store it in a database and retrieve it and put it in the session each time the user logins in (depending on size, etc.) If it is global data (for all users) you may want to both persist it in a database and store it in the common cache. A singleton class just to store data is really no different than the cache, except not as functional. Using the cache allows the data to automatically be removed when stale with mechanisms for reloading as needed.
tvanfosson
Recreate SQL connections as needed. Do not keep them around. The connection pool manager will take care of connection sharing and lifetime. Your app does not need to do this.
tvanfosson
You can also tell ASP.Net Cache to use SQL as the backing store. You can subscribe to the appropriate event to know when your object is removed from cache.
SharePoint Newbie
+3  A: 

Variable scope doesn't have anything to do with values being held between sessions.

public scope just means that the variable can be seen by any other code in your application.

If you want the object to be able to be stored across sessions you need to store it in the Session.

Joseph
+3  A: 

Hi,

  1. Every Page is an instance of the WebPage class, which is destroyed and recreated on every request.
  2. The session bag is unique to the session and is not shared across different sessions. It only stores stuff you add to it.

1 + 2 = No. Just adding a public field to a page does not add it to session. The same value does not hold between sessions.

Can you tell us the exact issue you are facing?

SharePoint Newbie
A: 

The only way to share data between sessions is by storing it in a database or in application variables.

Justin R.