Hi,
I need to remove the data stored in the cache after few minutes (Output and Data Cache).
My concept is binding data from server, storing it in to cache and displayed in the gridview, after a 10 seconds when the button is clicked the cache data should removed and need to show null like nothing. I have build some basic of code, Please any help to get perfectly. I am new to dotnet.
Please find the below required points and attachments of asp.dot net code and sql query.
1. When page load, gridview should display using Cache.
2. when Clear Cache Button after 10 second from pageload is clicked then the gridview should go hide
3. Reload Cache Button is clicked the gridview again load.
Default.aspx
Default.aspx.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data.SqlClient;
usingSystem.Data;
publicpartialclass _Default :System.Web.UI.Page
{
protectedvoidPage_Load(object sender,EventArgs e)
{
loadgrid();
}
publicvoid loadgrid()
{
SqlConnection con;
DataView gv1;
gv1 =(DataView)Cache["mysource"];
if(gv1 ==null)
{
string str ="Data Source=MyPC; Initial Catalog=Database; Integrated Security=true";
con =newSqlConnection(str);
con.Open();
stringselect="select * from cache";
SqlCommand cmd =newSqlCommand(select, con);
SqlDataAdapter da =newSqlDataAdapter(cmd);
DataSet ds =newDataSet();
da.Fill(ds);
gv1 =newDataView(ds.Tables[0]);
Cache["mysource"]= gv1;
Label1.Text="Data is Derived from DataBase and Stored in Cache";
}
else
{
Label1.Text="Data is Derived from Cache";
}
GridView1.DataSource=Cache["mysource"];
GridView1.DataBind();
}
}
SqlQuery
CREATE TABLE cache (id int, name varchar(15), role varchar(15))
insert into cache(1,'Name1','role1')
insert into cache(2,'Name2','role2')
insert into cache(3,'Name3','role3')
insert into cache(4,'Name4','role4')