0

Is it possible to create an Application State which can store DataSet()? I want to create DataSet variable that may be available for any user. How can I do that?

Thanks!

Ido
  • 397
  • 2
  • 7
  • 22
  • why do you want to give a adataset to user? – Saddam Abu Ghaida Feb 15 '14 at 13:31
  • I want every user who logs in to be added to the public table, which will contain his IP address, his username and the time he logged in. *I don't want to use database for this - I want to use an Application State variable * – Ido Feb 15 '14 at 13:33

2 Answers2

2

It is just a matter of setting

 if(Application["myGlobalDataset"] == null)
     Application["myGlobalDataset"] = LoadMyDataSet();

However, read carefully the MSDN (bold is mine)

Application state is a data repository that is available to all classes in an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.

A good place to initialize the variable is in the Application_Start event found in the global.asax.cs file

void Application_Start(object sender, EventArgs e) 
{
     if(Application["myGlobalDataset"] == null)
        Application["myGlobalDataset"] = LoadMyDataSet();
}

and remember to destroy it in the Application_End event

void Application_End(object sender, EventArgs e) 
{
    //  Code that runs on application shutdown
     if(Application["myGlobalDataset"] != null)
        Application["myGlobalDataset"].Dispose();
}

If you plan to modify the content of the Dataset keep in mind that you need to prevent concurrent access to the variable and thus a locking mechanism is required.

 try
 {
     Application.Lock()
     Dataset ds = Application["myGlobalDataset"] as Dataset;
      ......
  }
  finally
  {
      Application.UnLock()
  }

The Lock persists until the page terminates processing or times out, however I still prefer to enclose everything in a try/finally block

Steve
  • 213,761
  • 22
  • 232
  • 286
  • And now in order to add a new row to the the table I just need to consider the Application variable as a DataSet variable? – Ido Feb 15 '14 at 13:36
  • Now if you plan to modify this dataset then other problem arises like concurrent access to the same variable. – Steve Feb 15 '14 at 13:37
  • I can't find the global.asax file. How do I create one? – Ido Feb 15 '14 at 13:50
  • Right click on Solution explorer and select Add New item, then add the Global Application Class item – Steve Feb 15 '14 at 13:58
0

So you just want to store the DataSet in an application variable?

Application["YourDataSet"] = YourDataSetVariable.

From your other comments it is probably best to store it in a Session variable since it seems you are going to keep updating the information.

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
user3241191
  • 505
  • 2
  • 5
  • 12