0

System.Web.Caching.Cache and error on my page

by volkanuzun 4. June 2008 11:23

I am working on this web site, that does a lot of database queries, so i decided to use some caching mechanism here.

I do have a static function which reads the database, create a collection out the result and returns it to the caller, below is the simple code :

  private static List<WebSite> GetWebSites()
    {
        List<WebSite> WebSites = (List<WebSite>)Cache["WebSites"];
        if (WebSites == null)
        {
            DatabaseAccess db = new DatabaseAccess(ConfigurationManager.ConnectionStrings["CSUWebSitesConnectionString"].ConnectionString);
            WebSitesTB webSitesTB = new WebSitesTB(db);
            WebSites = webSitesTB.GetAllWebSites();
            Cache["WebSites"] = WebSites;
        }
     ......

but i got the error:  Error    39    An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Cache.get'    D:\WORK PROJECTS\CSUSBWebSitesApp\WebSitesApp\Administration\WebSites.aspx.cs    43    49    D:\...\WebSitesApp\

Can you guess what the problem is ? 

The problem is: Cache is a property of the class Page, and i am trying to access a property of page class, from a static function ; however this is not possible. What you should do is, change Cache to HttpContext.Current.Cache

 

Tags:

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Original Design by Laptop Geek, Adapted by onesoft