Issue I had while caching a Dataset in ASP.Net

Today I had a bug where pricing on my application was changing every time you clicked the button to search for product.  After running it through the debugger, I discovered that the cached Dataset was retaining the mark ups every time they were applied.  Here is a detail of what was happening:

When the user made a request for product I was going out to get the product based on their user settings (Who the were, what mark up was defined for their company, what market do the do business in, etc.)  These are generic settings that will not change during the session so i was caching this Dataset to alleviate going back to the DB every time.

Next, they would refine there search by the product criteria.  At this point I would get the Dataset out of cache, filter based on the criteria they chose and apply the mark up to the values in the Dataset.

Well, the caching holds a reference to the Dataset so any changes I made to the price in the Dataset was reflected in the cache.  So, if the user performed a search that returned any of those same products, the mark up was applied to the price in the Dataset, which had already been marked up.  So, depending on how many time they searched for the same product, that product got marked up that many times.  Big problem!

To alleviate this I simply copied the Dataset into a new one before I did my mark up.  That way the mark up was never reflected in the cached Dataset.  So the next time that data was received from the cache it still had it’s original price.  Very tricky to spot.  I’m glad it’s over.

Posted in |