larryq
larryq

Reputation: 16309

ASP.Net HttpContext Cache-- why do I read Null when someone else says ""?

I have a coworker who's written the following line in the page load method in an aspx page:

myDataSet = (DataSet)HttpContext.Current.Cache["dataset"];

The first time I hit the page HttpContext.Current.Cache["dataset"] reads null. When he does it, the value is "" (string.Empty) and he gets a cast exception.

We're both running ASP.Net 2.0 on our development machines, he's cleared his browser cache and run an iisreset, yet that thing still reads "" the first time he hits the page. Does anyone have ideas on what we can check to explain this discrepancy?

Upvotes: 0

Views: 778

Answers (3)

Aaron Daniels
Aaron Daniels

Reputation: 9664

I'd search your code and see what is actually assigning "dataset" into the cache. Something's got to be putting an empty string in there. Finding that may lead you to some other code that would explain the different results.

Without any real code samples, it's hard to troubleshoot.

Upvotes: 1

Greg Hurlman
Greg Hurlman

Reputation: 17804

Try this instead for now, you'll at least avoid hitting the exception:

myDataSet = HttpContext.Current.Cache["dataset"] as DataSet;

Upvotes: 1

David Glass
David Glass

Reputation: 2384

Perhaps you should try to use HttpRuntime.Cache instead of the HttpContext.Current.Cache.

http://theengineroom.provoke.co.nz/archive/2007/04/27/caching-using-httpruntime-cache.aspx

Difference between HttpRuntime.Cache and HttpContext.Current.Cache?

Upvotes: 0

Related Questions