GurdeepS
GurdeepS

Reputation: 67243

Object reference required for non-static field, method, or property

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

Upvotes: 5

Views: 10213

Answers (3)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422132

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.

Upvotes: 17

Dan Diplo
Dan Diplo

Reputation: 25349

Try this (from memory):

HttpApplication.Context.Cache.Insert("Reading List", datatable, sqlcachedep);

Upvotes: 1

womp
womp

Reputation: 116987

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.

Upvotes: 1

Related Questions