SQLcache Dependency and cache API

Items in the cache are kept up-to date with the changes in the database. This code sample explains to programmatically specify the cache and set their attributes.This is known as Programmatic output caching.

Event fired when static methods of the Cache class such as Insert, Remove, Add and so on gets started.

Defining key for sqldependency in web.config

<sqldependency enabled="true" pollTime="1000">
<add name="consdb" connectionStringName="consdb" pollTime="1000"/>

The code behind file is given below

Response.Write("Page created on:" + DateTime.Now.ToString());
DataSet ds= (DataSet)Cache["ds"];

if (ds== null)
SqlConnection conn = new SqlConnection("server=royalserver;database=db;UID=uid;PWD=pwd");
SqlDataAdapter adapter = new SqlDataAdapter("select * from tablename", conn);
ds= new DataSet();
//consdb is the key for sqldependency configured in web.config
SqlCacheDependency depend = new SqlCacheDependency("consdb", "ds");

//caache API--Starts here
//cache API--Ends here

Cache.Insert("ds", ds, depend);
Clients retrieved from the database");
Categories retrived from the cache");
gridView.DataSource = climst;


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: