Friday, 9 March 2018

Use of caching in Asp.net (GidView Demo)

Post Content
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.IO;
using System.Web.Caching;
public partial class _Default : System.Web.UI.Page
{
    string strXmlAllDetails = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getData();
        }
    }
    public DataSet getData()
    {
        DataSet dsAllDetailsOutput = (DataSet)Cache["mydata"];
        dsAllDetailsOutput = new DataSet();
        WebServiceDemoExample objWebServiceDemoExample = new WebServiceDemoExample();
        string strXmlOutput = objWebServiceDemoExample.FetchAllDetailsAdmin();
        XmlReader objXmlReader = System.Xml.XmlReader.Create(new StringReader(strXmlOutput));
        dsAllDetailsOutput.ReadXml(objXmlReader);
        Cache.Insert("mydata", dsAllDetailsOutput, new CacheDependency(Server.MapPath("")), DateTime.Now.AddHours(12), Cache.NoSlidingExpiration);

        if (dsAllDetailsOutput.Tables.Count > 0)
        {
            if (dsAllDetailsOutput.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = dsAllDetailsOutput;
                GridView1.DataBind();

            }
        }
        return dsAllDetailsOutput;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataSet ds = (DataSet)Cache["mydata"];
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}