Reputation: 11
I m having a webService. In that I m using Caching.
I have wrote following code to store datatable in cache.
using System.Web.Caching;
Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
It give me error like "An object Reference is required for non static field.
How can i remove this error
Upvotes: 1
Views: 515
Reputation: 39255
Use System.Web.HttpRuntime.Cache.Insert(...). This 'Cache' is a property returning an instance of System.Web.Caching.Cache. It's the same as HttpContext.Current.Cache, but without requiring an HttpContext.
Your code was trying to access a method of that class, without using an instance - thus the error message.
Upvotes: 3
Reputation: 64933
You're trying to use Cache class as an static one.
If you want to use current Cache class instance for an HTTP context during a request, you should be doing something like:
HttpContext.Current.Cache.Insert("dt", dt, null, DateTime.Now.AddHours(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
Upvotes: 4