Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));

Step: 1 Create website

Step: 2 Code

Web.config

<connectionStrings>

<add name="PlayerCnn" connectionString="Data Source=MANAS6\SQLEXPRESS;Initial Catalog=Players;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<identity impersonate="true" userName="Administrator" password="Manas6"/></system.web>

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.CacheDataSet1: </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
 

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
// SqlConnection
using System.Data.SqlClient;
//DataTable
using System.Data;
using System.Web.Caching;

public partial class _Default : System.Web.UI.Page
{
private string str_cnn = WebConfigurationManager.ConnectionStrings["PlayerCnn"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{

// Get playerTable from Cache
DataTable playerTable = (DataTable)Cache["Playerinfo"];
Trace.Warn("DataTable Cached " + DateTime.Now);
// If playerTable not in cache, recreate playerTable
if (playerTable == null)
{
// calling method
playerTable = GetPlayersFromDB();
Cache.Insert("Players", playerTable, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));
}
Trace.Warn("Binding to GridView");
GridView1.DataSource = playerTable;
GridView1.DataBind();
}
private DataTable GetPlayersFromDB()
{
Trace.Warn("Getting playerTable from database");
//string conString = WebConfigurationManager.ConnectionStrings["Players"].ConnectionString;
SqlDataAdapter dad = new SqlDataAdapter("SELECT * from Playerinfo", str_cnn);
DataTable playerTable = new DataTable();
dad.Fill(playerTable);
return playerTable;
}
}
 

Step: 3 runtime view