views:

42

answers:

3

Hi

Is it possible to set a variable in the system which is not for each user unique?, who access the page?

Ex.

I access the page and in codebehind something like that:

// create variable over all
if (sysstring != null || "")
SystemString sysstring = DateTime.now;

So if another user already accessed the page, I receive the value of the date when he accessed the page.

Thank you

+4  A: 

You're looking for Application scope:

string lastAccess = (DateTime)Application["lastAccess"];

Altho this will reset with every app recycle. I would suggest storing it in a DB, which is where all cross-user variables should be!

JustLoren
thank you i will try
snarebold
A: 

You can use the Application object:

HttpApplicationState app = this.Context.Application;
DateTime myValue = null;
app.Lock();
try
{
    myValue = (DateTime)app["key"];
    if (myValue == null)
    {
        myValue = DateTime.Now;
        app["key"] = myValue;
    }
}
finally
{
    app.UnLock();
}
RickNZ
A: 

Why not just make this static?

static string sysstring;

if (string.IsNullOrEmpty(sysstring)) sysstring = DateTime.Now;

As 'Loren said, just about anything other than storing this in the database will be lost when the app recycles.

Brad