5

I want to use the Caching.Cache(...) method, like so:

Cache.Insert("Interview Questions", datatable, sqlcachedep)

or

System.Web.Caching.Cache.Insert("Reading List", datatable, sqlcachedep);

There is no problem with the variables, but I get this error message in either case:

Error 1 - An object reference is required for the non-static field, method, or property 'System.Web.Caching.Cache.Insert(string, object, System.Web.Caching.CacheDependency)'

How can I fix this?

Thanks

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
GurdeepS
  • 65,107
  • 109
  • 251
  • 387

3 Answers3

17

It's saying the correct thing. You should try something like:

HttpContext.Current.Cache.Insert(...);

Cache.Insert is a not a static method (static methods are indicated by an "S" near the method icon in the documentation.) You need an instance to call the Insert method on. HttpContext.Current.Cache returns the Cache object associated with the current application.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
1

You need to do

Page.Cache.Insert()

(I'm assuming you're talking ASP.Net). You're calling on Cache as the class, not as the instance of it.

womp
  • 115,835
  • 26
  • 236
  • 269
1

Try this (from memory):

HttpApplication.Context.Cache.Insert("Reading List", datatable, sqlcachedep);
Dan Diplo
  • 25,076
  • 4
  • 67
  • 89