asp.net.ph

Skip Navigation LinksASP.NET Applications > ASP.NET Optimization > ASP.NET Caching Features > Caching Application Data > Retrieving Values of Cached Items

Retrieving Values of Cached Items

ASP.NET Web Applications   ASP .NET Caching Features   Caching Application Data


Retrieving data from the Cache is simple; you need only to specify the key that represents the data. You then write code to display the data on your page.

To retrieve the value of a cached item

  • The following code retrieves data that is assigned a key of myDataSet and assigns its value to a local variable named myData, verifies that the data still exists in the cache, and then displays the data in a DataGrid control.

DataView myData;
myData = ( DataView ) Cache [ "myDataSet" ];

if ( myData != null )
   myGrid.DataSource = myData;
   myGrid.DataBind ( );
}
  C# VB

To check for the existence of an item in the cache

  • A more complex situation is how your application responds when the data is not in the cache. The following example modifies the code in the previous procedure to check for the cached data, and if it is not in the cache, the example recreates it and adds it to the cache.

DataView myData = ( DataView ) Cache [ "myDataSet" ];
if ( myData == null )
   SqlConnection myConn = new SqlConnection ( 
      "server=localhost;uid=sa;pwd=;database=pubs" );
   SqlDataAdapter myCommand = new SqlDataAdapter ( "select * from Authors", myConn );

   DataSet ds = new DataSet ( );
   myCommand.Fill ( ds, "Authors" );

   myData = new DataView ( ds.Tables [ "Authors" ] );
   Cache [ "myDataSet" ] = myData;
}
myGrid.DataSource=myData;
myGrid.DataBind ( );
  C# VB

See Also

Adding Items to the Cache   Deleting Items from the Cache   Notifying Applications When an Item is Deleted from the Cache



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note