Click here to Skip to main content
15,891,567 members
Articles / Web Development / ASP.NET

Caching in ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.86/5 (36 votes)
17 Apr 20022 min read 159.4K   1.8K   53  
Demonstrates data caching in ASP.NET
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
  <script language="VB" runat="server">

    Sub Page_Load(Src As Object, E As EventArgs)

       Dim dataSource As DataView

'Retrieve item from cache if anything is not there then we have to add to the cache
        

	dataSource = Cache("CheDataSet")

        If dataSource Is Nothing

            Dim Conn As SqlConnection
            Dim Cmd As SqlDataAdapter

            Conn = New SqlConnection("server=database;uid=sa;pwd=;database=Northwind")
            Cmd = New SqlDataAdapter("select * from customers", Conn)

            Dim ds As New DataSet
            Cmd.Fill(ds, "dsUsers")

            dataSource = New DataView(ds.Tables("dsUsers"))
            Cache("CheDataSet") = dataSource

            CacheMsg.Text = "Dataset was Created directly from the Datebase. Here the Cache was not used but the Cache was Created,so check for the performence next time."
        Else
	    
            cacheMsg.Text = "Dataset was Retrieved from cache which was created earlier."
        End If

        MyDataGrid.DataSource=dataSource
        MyDataGrid.DataBind()
    End Sub

  </script>

  <body>

    <form method="GET" runat="server">

      <h3><font face="Verdana">Caching Data Example</font></h3>
	<i><asp:label id="CacheMsg" runat="server"/></i>

      <ASP:DataGrid id="MyDataGrid" runat="server"
        Width="700"
        BackColor="#Eaccff"
        BorderColor="black"
        ShowFooter="false"
        CellPadding=3
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#ffaad" />

      <p>

      

    </form>
  </body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
India India
I am working on .Net Techlology Extensively. I am presently in Bangalore.

Comments and Discussions