Tuesday 14 October 2014

How to use Caching Dataset in Asp.Net

How to use Caching Dataset in Asp.Net…….

Cache in Asp.Net:  Caching is a most important to use in Asp.Net web application. It is a technique of storing frequently used data/information in memory, so that, when the same data/information is needed next time, it could be directly retrieved from the memory instead of being generated by the application. , this is an effective way for improving web application’s performance.

For Example:

First we will declare Connection String in web.config ………………………..

<configuration>
<connectionStrings>
<add name="strconn" connectionString="Data Source=KUSHTIWARI-PC;Initial Catalog=test;Integrated Security=True"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>

Code for Default.aspx……………………………………………….

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

<!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></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
height: 22px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td colspan="2" style="color:Red">
<center><b><i> Caching DataSet in Asp.Net</i></b></center>    </td>
</tr>
<tr>
<td colspan="2">
<center><asp:GridView ID="GridView1" runat="server">
</asp:GridView></center>
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="btnloaddata" runat="server" onclick="btnloaddata_Click"
Text="Load Data" />
</td>
<td>
<asp:Button ID="btnClearCache" runat="server" onclick="btnClearCache_Click"
Text="Cache Clear" />
</td>
</tr>
<tr>
<td align="center" class="style2" colspan="2">
<asp:Label ID="lblmessage" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>


Code for 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 namespace.....
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
// code  fro  how to apply cache........

protected void btnloaddata_Click(object sender, EventArgs e)
{
if (Cache["empdata"] == null)
{
string strcon = ConfigurationManager.ConnectionStrings["strconn"].ConnectionString;
using (SqlConnection con = new SqlConnection(strcon))
{
SqlCommand cmd = new SqlCommand("Select * from emp", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);


lblmessage.Visible = true;
lblmessage.ForeColor = Color.Blue;
lblmessage.Text = "This data are coming from Sql server ";

Cache["empdata"] = ds;
GridView1.DataSource = ds;
GridView1.DataBind();

}

}
else
{

 GridView1.DataSource = (DataSet)Cache["empdata"];
GridView1.DataBind();

lblmessage.Text = "";
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Blue;
lblmessage.Text = "This data are coming from Cache";
}


}
// code for clear cache............................
protected void btnClearCache_Click(object sender, EventArgs e)
{
if (Cache["empdata"] != null)
{
Cache.Remove("empdata");
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "The Dataset removed from cache";

}
else
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "There is nothing in the Cache to be removed";
}
}
}


Result: 



After that when you will stop the Sql server  Services from the steps given bellow 

Step 1 – press window Button + R
Step 2 - write services.msc in Textox and press OK button you will see new window like..



After stopping Sql Server Services and again click LoadButton Button you will find all data because data are coming from cache because data is store in cache but when you will click CacheClear Button  then again click LoadData Button  you will find this type error ..

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)


0 comments:

Post a Comment