ASP.NET (35) SQL (25) JAVASCRIPT (24) HTML (14) STYLE SHEET (6) ASP (4) SCRIPT (1)

Search me out HERE!!

Fill DATASET from Cache in ASP.net

Below code will show how to fill DataSet from cache instead of getting data each time from Database:


public DataSet load_data()
    {
        DataSet ds;
        if (Cache["test"] == null)
        {
            SqlConnection cn = new SqlConnection("server=.;database=pubs;uid=sa;password=;");
            cn.Open();
            SqlDataAdapter cmd = new SqlDataAdapter("select * from test",cn);
            ds = new DataSet();
            cmd.Fill(ds, "test");
            Cache.Insert("test", ds, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));
            cn.Close();
        }
        else
        {
            ds = (DataSet)Cache["test"];
        }
        return ds;
    }

You can get your Dataset by just calling load_data() method as given below:
DataSet ds = load_data();

1 comment: