c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
ADO.NET

What is caching in dataset and how to implement in asp.net c#?

| | ASP-NET , CSharp

Caching is a technique to store the data in memory using dataset object. It improves scalability and performance to the web application. Below example we will be implementing the caching technique.

We will be using ProductDetail table.

Step 1: Create a table using the following script with data:

CREATE TABLEProductDetail 
(
 ProductId int identity primary key,
 ProductName nvarchar(50),
 UnitPrice int
)
INSERT INTOProductDetail VALUES('Lenova',523)
INSERT INTOProductDetail VALUES('Nokia 520',550)
INSERT INTOProductDetail VALUES('Micromax',560)
INSERT INTOProductDetail VALUES('Samsung Galaxy S5',926)
INSERT INTOProductDetail VALUES('Sony',450)

Step 2: Copy and paste the following code.

Default.aspx:

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <table style="border: 1px solid #e2e2e2; font-family: Arial">
       <tr>
            <td>
                <asp:Button ID="btnLoadData" runat="server" Text="Load Data"
                   OnClick="btnLoadData_Click" />
                <asp:Button ID="btnClearnCache" runat="server" Text="Clear Cache"
                   OnClick="btnClearCache_Click" />
                <br />
                <br />
                <asp:Label ID="lblMessage" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="padding: 10px 5px 5px 40px">
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            </td>
        </tr>
    </table>

Default.aspx.cs: 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           BindData();
        }
    }
    protected void btnLoadData_Click(object sender, EventArgs e)
    {
        // Check if the DataSet is presentin the cache
        if (Cache["Data"] == null)
        {
           BindData();
        }
        // If the DataSet is in the Cache
        else
        {
            // Retrieve the DataSet from theCache and type cast to DataSet
           GridView1.DataSource = (DataSet)Cache["Data"];
           GridView1.DataBind();
           lblMessage.Text = "Dataloaded from the Cache";
        }
    }
 
    protected void btnClearCache_Click(objectsender, EventArgs e)
   {
        // Check if the DataSet is presentin the cache
        if (Cache["Data"] != null)
        {
            // Remove the DataSet from theCache
           Cache.Remove("Data");
           lblMessage.Text = "DataSetremoved from the cache";
        }
        // If the DataSet is not in theCache
        else
        {
           lblMessage.Text = "Thereis nothing in the cache to remove";
        }
    }
   private void BindData() {
        string ConnString = ConfigurationManager.ConnectionStrings["ShoppingZone"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(ConnString))
        {
           connection.Open();
            string str = "Select * fromProductDetail;";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(str, connection);
           dataAdapter.SelectCommand.CommandType = CommandType.Text;
            DataSet dataset = new DataSet();
           dataAdapter.Fill(dataset);
           dataset.Tables[0].TableName = "ProductDetails";
           GridView1.DataSource = dataset.Tables["ProductDetails"];
           GridView1.DataBind();
            // Store the DataSet in the Cache
           Cache["Data"] = dataset;
           lblMessage.Text = "Dataloaded from the Database";
        }
    }
} 

Output: