views:

34

answers:

2

hi, in my application i trying to implement the cache (ouput) but it is not working fine, that is it is every time getting from cache only this is my code.

<%@ OutputCache VaryByParam ="none" Location="Client" Duration="10" %>.

Code:

protected void btn_Click(object sender, EventArgs e)
    {
        DataView dtv;
        dtv = (DataView)Cache["mycache"];
        if(dtv ==null )
        {
            string sqry="select * from scrap";
            da=new SqlDataAdapter (sqry,con);
            ds=new DataSet();
            da.Fill (ds);
            dtv=new DataView (ds.Tables[0]);
            Cache["mycache"]=dtv ;
            Response.Write ("<script> alert ('from code')</script>");
        }
        else 
        {
             Response.Write ("<script> alert ('from cache')</script>");
        }
        grd1 .DataSource =dtv;
        grd1 .DataBind();
+2  A: 

The OutputCache and the Page.Cache are in no way related. The OutputCache caches the html that the page generates and returns that to the browser without running your code again (for 10 seconds as by your current configuration). The Page.Cache provides a mechanism for storing application wide data. Once something is added to that cache it will be there until the next time you restart your website (unless explicitly removed).

klausbyskov
+1  A: 

Yes, @klausbyskov is right. Try using the Cache.Insert() method overload with the expiration argument for setting timeout on the data cache elements.

bjornhol