Hi Friends,
Here we learn how we increase the speed of our application by using caching.
Lets start what we will done.
we create a simple application where using a gridview and bind with database and using cache for speed our application.
Lets Start
The codes of .cs
using System;
using System.Data;
using System.Web.Caching;
/* Please remember to import SqlClient namespace */
using System.Data.SqlClient;
public partial class GridView_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* Check whether the page is post back or not */
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
if (Cache["Cache"] == null)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=DataDirectorymysql.mdf;Integrated Security=True;User Instance=True");
SqlDataAdapter ad = new SqlDataAdapter("select * from sqltest", con);
DataSet ds = new DataSet();
ad.Fill(ds);
Cache.Insert("Cache", ds, null , DateTime.Now.AddMinutes(2), TimeSpan.Zero);
GridView1.DataSource = ds;
}
else
GridView1.DataSource = (DataSet)Cache["Cache"];
GridView1.DataBind();
}
}
Subscribe to:
Post Comments (Atom)
5 comments:
Good Stuff
What are you in second grade?:D
The blog is helpfull...
visit also asp.net [c#]
Thank you very much Qureshi
Very good,
a very simple way of doing,
congratulations ...
Post a Comment