raklos
raklos

Reputation: 28545

save and retrieve values within web app

I have a simple web app built in asp.net webforms c#

where and how would be the best way to save info from the code behind? (and also retrieve that info)

eg all i want to save is a dateTime. and a flag set to True or False. and be able to access them in the code behind.

Im not using a db for this web app.

Edit: and can't really use session variables for this purpose.

thanks

Upvotes: 0

Views: 326

Answers (5)

Thibault Falise
Thibault Falise

Reputation: 5885

If you need to read the saved variable after a lengthy period of time, you should store it in a local file or a database.

You can create a file using a FileStream object and then write your value to the file.

To get a path where your application has sufficient rights to write a file, use Server.MapPath.

Note : If possible, and if the data you store should not be available to users, you should configure your IIS WebSite to forbid him to serve this file to clients.

Upvotes: 1

Oleks
Oleks

Reputation: 32323

If this is per user info you could use either browser cookie or viewstate.

Upvotes: 1

Usman Akram
Usman Akram

Reputation: 271

Use Application variable like Application["thedate"] = date; and you can get back as date = Application["thedate"]. The date will be saved untill app pool restarts (which also happens when IIS or system restarts).

For a more longer time, save in an xml file on the disk (Use XMLReader and XMLWriter for this purpose).

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

If you have to save data for an arbitrary period of time, then you need to store it in a database.

Upvotes: 3

David Morton
David Morton

Reputation: 16505

Use the Session object:

Session["thedate"] = date;

DateTime date = (DateTime)Session["thedate"];

Upvotes: 0

Related Questions