Hi
Using SqldataSource or ObjectdataSource control, we can easily implement the cache concept in Gridview. If we will use the cache concept,it will avoid the server overloading and it will increase the performance of website.
We can do like this.Here i m using objectdatasource.
Step1:Create one Class i.e “GetEmp” and write the code like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;///
/// Summary description for GetEmp
///public class GetEmp
{
private static readonly string _ConnectionString;
public int Id { get; set; }
public string EmpName { get; set; }
public string EmpSal { get; set; }static GetEmp()
{
_ConnectionString = WebConfigurationManager.ConnectionStrings[“Test”].ConnectionString;
}public List GetAllEmp()
{
List result = new List();
using (SqlConnection con = new SqlConnection(_ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(“Select Id,Empname,EmpSal from tblEmp”, con))
{
using (con)
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
GetEmp newEmp = new GetEmp();
newEmp.Id = (int)dr[“Id”];
newEmp.EmpName = (string)dr[“EmpName”];
newEmp.EmpSal = (string)dr[“EmpSal”];
result.Add(newEmp);}
}
}
return result;
}
}
}
Step2: Take one Gridview and ObjectDatasouce Control in default page like this.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1" CellPadding="4" ForeColor="#333333">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName"
SortExpression="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal"
SortExpression="EmpSal" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" TypeName="GetEmp" EnableCaching="true" CacheDuration="3000" SelectMethod="GetAllEmp" runat="server">
</asp:ObjectDataSource>
Step3:Now Compile the code.