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

Posted on 2009-02-16
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 function which would be a lot faster than a calling the database each time a Product_ID is passed.

Any help would be appreciated...


Question by:nkewney
    LVL 19

    Expert Comment

    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.
    LVL 5

    Accepted Solution

    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.
    LVL 5

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now