views:

168

answers:

3

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?

A: 

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

http://stackoverflow.com/questions/863654/difference-b-w-httpruntime-cache-vs-httpcontext-current-cache

David Glass
Thanks for the tips everyone-- appreciate it!
larryq
+1  A: 

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

myDataSet = HttpContext.Current.Cache["dataset"] as DataSet;
Greg Hurlman
+1  A: 

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.

Aaron Daniels