Welcome to Idea R | Branding - Web Agency - Digital Strategies
Switch to the mobile layout

      Idea R - Plan your visual communication at 360°

Blog

Take a software geek from the 80s, add a new generation graphic designer and dilute with a longtime marketing strategist. Shake vigorously and you'll get the Idea R's blog.

Change language... italiano

How to clear the SqlDataSource cache

Published on 12/24/2011
Categories: Web & Apps

The SqlDataSource.EnableCaching property allows to save the results of a specific database query.
When the caching is activated, if you reset EnableCaching to false, the query result is not retrieved from the cache anymore but it's re-executed every time.
But surprisingly, if you reset EnableCaching to true again, the data is retrieved from the old cache.

// The Customers table contains PIPPO as the name of the customer 1
SqlDataSource dataSource = new SqlDataSource(myConnectionString, "SELECT Name FROM Customers WHERE CustomerID = 1");
dataSource.EnableCaching = true;
// The query returns PIPPO
...
// Update the database changing the name of the customer 1 to MINNY
...
dataSource.EnableCaching = false;
// This time the cache is disabled, so the query returns MINNY
...
dataSource.EnableCaching = true;
// WARNING! The query returns the old and expired value PIPPO again

To clear the cache then it is useless to set EnableCaching to true, but we must proceed using the SqlDataSource.CacheDependency properties.

// The Customers table contains PIPPO as the name of the customer 1
SqlDataSource dataSource = new SqlDataSource(myConnectionString, "SELECT Name FROM Customers WHERE CustomerID = 1");
dataSource.EnableCaching = true;
dataSource.CacheKeyDependency = "MyCacheKey";
if (Cache["MyCacheKey"] == null] Cache["MyCacheKey"] = DateTime.Now;
// The query returns PIPPO
 
...
 
// Update the database changing the name of the customer 1 to MINNY
 
...
 
Cache["MyCacheKey"] = DateTime.Now; // Clear the cache
// Now the query correctly returns MINNY

 

Google Reviews

Do you like this blog?

For us your opinion matters, so we would be very pleased if you could write a review about us or our blog.

You are the reader number 15,168.

Comments

Previous article

Previous article

Dropdown menus with CSS

Next article

Reflections on the water with Photoshop

Next article

Scroll to top