<%@ Page Language=
"C#"
%>
<%@ Import Namespace=
"System.Data.SqlClient"
%>
<%@ Import Namespace=
"EmployeesDataSetTableAdapters"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<script runat=
"server"
>
protected
void
Page_Load(
object
sender, EventArgs e)
{
string
dateTimeNow = DateTime.Now.ToString();
Label1.Text = dateTimeNow;
Label2.Text = dateTimeNow;
SqlCacheDependency dependency =
null
;
string
connectionString =
ConfigurationManager.
ConnectionStrings[
"Northwind2"
].ConnectionString;
if
(Cache[
"EmployeesDataTable"
] ==
null
)
{
try
{
dependency =
new
SqlCacheDependency(
"Northwind"
,
"Employees"
);
}
catch
(DatabaseNotEnabledForNotificationException)
{
try
{
SqlCacheDependencyAdmin.
EnableNotifications(connectionString);
}
catch
(UnauthorizedAccessException)
{
Response.Redirect(
"ErrorPage.htm"
);
}
}
catch
(TableNotEnabledForNotificationException)
{
try
{
SqlCacheDependencyAdmin.
EnableTableForNotifications(
connectionString,
"Employees"
);
}
catch
(SqlException)
{
Response.Redirect(
"ErrorPage.htm"
);
}
}
finally
{
EmployeesTableAdapter adapter =
new
EmployeesTableAdapter();
EmployeesDataSet.EmployeesDataTable table =
adapter.GetData();
GridView1.DataSource = table;
GridView1.DataBind();
Cache.Insert(
"EmployeesDataTable"
,
table, dependency,
DateTime.Now.AddMinutes(60),
Cache.NoSlidingExpiration);
}
}
else
{
GridView1.DataSource =
(EmployeesDataSet.EmployeesDataTable)
Cache[
"EmployeesDataTable"
];
GridView1.DataBind();
}
}
</script>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head id=
"Head1"
runat=
"server"
>
<title>SQL Cache Dependency</title>
<style type=
"text/css"
>
#UpdatePanel1
{
width: 250px;
height: 370px;
border: gray 1px solid;
margin-left: 10px;
margin-top: 10px;
padding: 0 5px 0 5px;
background-color: #eeeeee;
}
</style>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<h1>SQL Cache Dependency</h1>
<asp:ScriptManager ID=
"ScriptManager1"
runat=
"server"
>
</asp:ScriptManager>
<asp:Label ID=
"Label1"
runat=
"server"
>
</asp:Label>
<asp:UpdatePanel ID=
"UpdatePanel1"
runat=
"server"
>
<ContentTemplate>
<p>UpdatePanel</p>
<hr />
<p><asp:Label ID=
"Label2"
runat=
"server"
>
</asp:Label></p>
<asp:GridView ID=
"GridView1"
runat=
"server"
AutoGenerateColumns=
"False"
DataKeyNames=
"EmployeeID"
EnableModelValidation=
"True"
EnableViewState=
"False"
BackColor=
"White"
>
<Columns>
<asp:BoundField
DataField=
"EmployeeID"
HeaderText=
"EmployeeID"
InsertVisible=
"False"
ReadOnly=
"True"
SortExpression=
"EmployeeID"
/>
<asp:BoundField
DataField=
"LastName"
HeaderText=
"LastName"
SortExpression=
"LastName"
/>
<asp:BoundField
DataField=
"FirstName"
HeaderText=
"FirstName"
SortExpression=
"FirstName"
/>
</Columns>
</asp:GridView>
<asp:Button ID=
"Button1"
runat=
"server"
Text=
"Refresh Panel"
/>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>