We help IT Professionals succeed at work.

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

Medium Priority
Last Modified: 2013-12-17
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...


Watch Question

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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.