Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Cache the DataSet as an XML file

Author: Aadil

This article will show how to manually cache data on the server in an xml file without using the Cache object.

In ASP.NET there is an object called Cache that you can use to store objects that can be accessed by multiple users. This object stores the contained data in the memory of the web server and is quire reliable because at peak levels when the server is utilizing lot of memory then ASP.NET will automatically removes objects contained in this object and the web server can utilize this memory.

 

We may need sometimes a caching mechanism that can be used to persist the data to the file system and not in the web server’s memory like the ASP.NET’s Cache object. In this case we can use the DataSet object’s built in capabilities that allow us to load the DataSet from an xml file and save a DataSet in an xml file.

 

What I’ve done in this sample example is that I am getting a DataSet from a function and binding this with a DataGrid control. The function looks for an xml file on the same path where it is stored and if it finds one, it loads the DataSet from this file and of no file is found (if this is the first time this function is called) then it fetches the data and stores it in the xml file.

 

The ASP.NET web page code is as follows

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>title>

head>

<body style="font-family:Verdana; font-size:10pt;">

    <form id="form1" runat="server">

      <asp:DataGrid ID="dgOne" runat="server" Font-Size="11px" AutoGenerateColumns="true">

      asp:DataGrid>

    form>

body>

html>

The code for the code behind file is

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Me.dgOne.DataSource = Me.GetDataSet()

    Me.dgOne.DataBind()

  End Sub

 

  Public Function GetDataSet() As DataSet

    Dim DS As DataSet

    If File.Exists(MapPath("xmlDataSet.xml")) = False Then

      DS = Me.GetFreshDataSet()

      DS.WriteXml(MapPath("xmlDataSet.xml"))

      Return DS

    End If

   

    DS = New DataSet

    DS.ReadXml(MapPath("xmlDataSet.xml"))

    Return DS

   

  End Function

 

  'This function will connect to the database and gets the frsh data in a DataSet object

  Private Function GetFreshDataSet() As DataSet

    Dim objConn As New SqlConnection("Server=127.0.0.1;UID=aadil;Password=protection;Database=db2")

    objConn.Open()

 

    Dim DS As New DataSet

    Dim DA As New SqlDataAdapter("select * from employees", objConn)

    DA.Fill(DS, "Employees")

    DA.Dispose()

    DA = Nothing

   

    objConn.Close()

    objConn = Nothing

    Return DS

  End Function

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using a Bound Column in a DataGrid Control

Disclaimer - Privacy
© 2002-2012 DevASP.net