Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top