• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Storing a DataTable in a single instance on a server in C#

Dear Experts,

In my web application (ASP.NET 3.5), I have pricing data I call upon often.

I currently have a control which has the property Product_ID and retrieves the price from the database.

I'd like to store this data on a single instance on my server when the application starts and allow all user sessions to draw on it.

Is this something I could do with a singleton instance, and if so, could somebody describe or provide sample source to show me what I would need to do to implement this?

I could the filter down the data using dataset.search function which would be a lot faster than a calling the database each time a Product_ID is passed.

Any help would be appreciated...


  • 2
1 Solution
You could write a singleton but why not just use the Application object  - this is effectiveley a singleton and is written for this sort of thing.
I'd suggest use caching.

Enterprise Library provides the best (IMO) way to cache data, plus other very useful features.
I will strongly suggest you use that.

With Enterprise Library, you'll find separate blocks for Caching, Exception, Data, ... handling.
All you need to do is to add a reference to the Caching Block Dll(s), and in your code, do something like this:

void SetInitialCache(){
                CacheManager _Cache = CacheFactory.GetCacheManager();
     List<Prodcut> Product_ID = GetAllProductsFromDatabase();
                foreach( Product prod in Product_ID){
        _Cache.Add(prod.Key, prod, , CacheItemPriority.Normal, Nothing, New SlidingTime(TimeSpan.FromMinutes(5)));

Product GetProductById(string keyNeeded){
    //Check if _Cache is not set to null and its count > 0
    //If not, call SetInitialCache() and set its value.

I hope I am clear here, and this helps!

Btw, the help files for Enterprise library is very good, and you should have no problems implementing the same.

P.S: Please ignore any compilation errors that might happen, I don't have my dev environment setup on this machine.

Hope that helps.

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now