0

I'm creating a webserver using C# codebehind pages. What I want is for one user to view and submit something on the page, and that the data is stored serverside, in such a way that it's persistent, AND that ANOTHER user can view this data.

Am I restriced to saving it to a flatfile or database, or is there some kind of feature I can use in C# or .NET for this purpose, like a peristent cross-session variable or something? (normal static variables would be different for each different user, right?)

Henrik Kjus Alstad
  • 674
  • 2
  • 9
  • 24

4 Answers4

2

Any static variable will be common to all users, but you should use Application. It is like Session but common to all users. If you need persistence over application reset then use a database.

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
  • Exactly what I needed actually,thanks! I was convinced static variables were different for each user running the backend-code, but obviously it's tied to the webserver, not user-interactions – Henrik Kjus Alstad Jul 03 '12 at 16:00
  • HttpApplicationState is not persistent as it dies with the process that created it - the Application. – Stefan Keller Jul 03 '12 at 16:37
  • 1
    I didnt use the right word - persistance was not really what I wanted. The important thing for me was that the variable should be accessible to all users browsing the page, and that if one user modified the variable, another user refreshing the page would see the change. And of course that the variable would stay even if the users quit the webpage. Which it does when using static. It's only lost if the webserver goes down, which is fine – Henrik Kjus Alstad Jul 05 '12 at 08:57
1

You can test these caching variable

Cache["Key"] = yourValue;

Application["Key"] = yourValue;

You have another cache associated to the session, but it's not global:

Session["Key"] = value; 

You have another cache in your page

ViewState["Key"] = value;
forsvarir
  • 10,749
  • 6
  • 46
  • 77
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

If you don't have very much data, store it into memory, and write it to disk using either XmlSerializer or BinaryFormatter.

You will want to store your data in memory as an application singleton, accessible from everywhere in your set of pages.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
-1

Am I restriced to saving it to a flatfile or database, or is there some kind of feature I can use in C# or .NET for this purpose?

There can not be another feature. See, a flat file (i.e. a file you write or another service writes) and a database (which can be a SQL database, a document database or any other form around) logically cover all possibilities that there are for persisting data. I do not think there CAN be any other way, theoretically, that would not already fall into a flat file (i.e. a file you manage) or a database (a file some driver or server process manages).

What you ask is like "I have a radio, it can be on or off. Is there any other state it can be in?" - bad news, no, either it is on or off.

Any feature in the .NET framework either runs down to being either of the two you name at the end.

TomTom
  • 61,059
  • 10
  • 88
  • 148