Caching is a method used to increase the performance by keeping the frequently accessed data in memory. Here is the example for Data Caching using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class DataCaching : System.Web.UI.Page
{
SqlConnection objCon = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter objAda = new SqlDataAdapter();
DataSet objDs = new DataSet();
DataView objview;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
objview = (DataView)Cache["testCache"];
if (objview == null)
{
objCon.ConnectionString = "Data Source=.;Initial Catalog=Company;Integrated Security=True;";
objCmd.Connection = objCon;
objCmd.CommandText = "select * from emp";
objAda.SelectCommand = objCmd;
objAda.Fill(objDs, "emp");
objview = new DataView(objDs.Tables["emp"]);
Cache["testCache"] = objview;
lblData.Text = "Dataset from Table:";
}
else
{
lblData.Text = "Dataset from Cache:";
}
GridView1.DataSource = objview;
GridView1.DataBind();
}
}
}