asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Using DataSets Without a Database in ASP.NET
By Steve Schwarting
Rating: 3.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

  • download source code
  • Introduction

    Sometimes you don't have a database table to work with, but you still want your page to be dynamic in terms of the data it presents to the user. Meaning, you don't want to have to modify code in order to make the page display new information or information in a different way. You would prefer to have some aspects of the data that is presented on the page abstracted and separated from the page itself.

    In the days of classic ASP, I took advantage of XML files stored on the server to accomplish this. These XML files would store both the data and the metadata that the page needed to create a dynamic display. It would get read in by the page, parsed up and VBScript code would then make determinations about what to do or how to display it. If I needed to make a change, I would just modify the XML and without changing code the page would display the new or modified data.

    In ASP.NET there are even more opportunities you can take advantage of using this approach. For instance, you can store the information used by the page in a DataSet that has been serialized out to XML. This saved XML contains the schema for the table structure(s) as well as the data. The code behind your aspx page can simply read the XML in from disk using the ReadXml method of the DataSet object. From there, you can work with the DataSet object the same as if you had filled it via a SqlAdapter object that pulled the information from a SQL Server database. In addition, you could make use of ASP.NET's extremely useful caching features and make it dependant upon modifications to the XML file residing on the disk. Then instead of having to read it in from disk each time, you would only do it once, and until the file on disk changed, ASP.NET would store the DataSet in the server cache.

    Applications

    Of course, in most cases you'd ideally like to have a database table, but there are certain situations where this sort of method is a practical alternative. One such use is if you have a page that requires only a few or even one set of data elements. It may not be practical to create a table in your database to store that few number of rows. In fact, you may have your Database Administrator become obstinate about it and question your judgment about the matter, especially in the case where only one row is needed.

    Another use might be where the requirements for the page display change so rapidly that it does not yet make sense to create a table and then ask the DBA to alter it over and over again in an attempt to keep up with the demands of your demanding users. This puts you more in control and also provides a quick and flexible approach to getting changes out quickly until the chaos settles down. After that, you can quickly and easily take your DataSet and model and table (or tables) from that to get the data into a table in a database. What is great about this is very little code would have to be modified since the code can work with the DataSet object in the same way as it did before.

    A third use for this may be if you are hosting your site on an ISP server and they do not provide a database for you to work with. In this case, you are stuck with a dilemma and this provides an excellent alterative. Especially if the amount of data is not very large.

    Working Example

    Here is an example of the output from a simple page that pulls in product information from a DataSet stored on disk and displays it:

    Here is a look at the XML itself that is stored on disk. It represents a serialized DataSet containing a table called "Products" that has four rows.

    Here is a snippet of the VB.NET code of the page that reads it, sorts and filters it, and displays the data using a DataList. Notice, that it is also making use of the Cache object to store the DataSet in memory. This cache would expire if the physical contents of the file stored on disk were to change. In that instance, the next execution of the page would read in the new DataSet from disk again.

    Try
        ' Assume we are not going to show anything
        DataList1.Visible = False
        
        ' See if the product list is in cache
        dsProducts = CType(Cache(sCacheName), System.Data.DataSet)
    
        If dsProducts Is Nothing Then
            ' Get the path to the XML file containing the product list
            sPath = ConfigurationSettings.AppSettings("ProductListFile")
            ' Translate it to a physical file name
            sPath = Server.MapPath(sPath)
            ' Check to make sure that the the actual file exists
            If File.Exists(sPath) Then
                ' Populate the DataSet with the XML contents of the file
                dsProducts = New DataSet
                fs = New FileStream(sPath, FileMode.Open, FileAccess.Read)
                xtr = New System.Xml.XmlTextReader(fs)
                dsProducts.ReadXml(xtr, XmlReadMode.ReadSchema)
                xtr.Close()
                fs.Close()
                ' Populate cache with the DataSet and make its expiration
                ' dependant upon a change to the file
                Cache.Insert(sCacheName, dsProducts, New CacheDependency(sPath))
            Else
                Throw New System.Exception("Product File does not exist!")
            End If
        End If
        ' Now let us display the products if any
        If dsProducts.Tables("Products").Rows.Count > 0 Then
            dv = dsProducts.Tables(0).DefaultView
            ' Sort by name
            dv.Sort = "Name"
            ' Display only those that are ok to show
            dv.RowFilter = "Display = 1"
            If dv.Count > 0 Then
                DataList1.DataSource = dv
                DataList1.DataBind()
                DataList1.Visible = True
            End If
        End If
    Catch ex As System.Exception
        Throw ex
    End Try
    

    An additional add-on to this approach would be to create a page that allows editing of the data stored in the XML. You simply would read it in as before, manipulate the data stored in the DataSet, and then write the XML back out to the same location. This would save having to manually update the file itself each time there is a data change. It would also provide the ability for someone that is not familiar with XML, such as a content person, or non-technical website administrator, to update the data through a more pleasing user interface and effectively change what gets displayed on the product page shown above.

    You can download a zip file containing the complete working example from here.

    Conclusion

    To summarize, this approach is simply an alternative to using a DataSet populated from information stored in a database. It goes without saying that wherever possible, it makes much better sense to have your data stored in a table in a database. However, sometimes this is either not desirable or not possible. In these cases, using the above technique makes for a sensible solution.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Feb 23, 2005 - My Feature in Visual Basic 2005
    In this article, Thiru Thangarathinam demonstrates the different classes and features available through the My namespace. By providing a speed-dial that allows you to more quickly and effectively utilize .NET framework functionalities in your application, the My feature provides huge productivity improvements for .NET developers.
    [Read This Article]  [Top]
    Oct 6, 2004 - Creating Triggers Using Managed Code in SQL Server 2005
    Thiru Thangarathinam discusses taking advantage of the integation between the .NET CLR and SQL Server 2005 in order to do things like create triggers using managed code.
    [Read This Article]  [Top]
    Sep 8, 2004 - Custom Object Data Binding with .NET
    Developers often use brute force coding to marshal data between the GUI and application objects. In this article, Luther Stanton explains how to use .NET's out-of-the box data-binding functionality to make this job much easier.
    [Read This Article]  [Top]
    Aug 17, 2004 - The Perfect Service - Part 2
    Ambrose Little provides the complete source code for his 'Perfect Service' and explains how the .NET Service Manager enables features such as drag-n-drop deployment.
    [Read This Article]  [Top]
    Aug 12, 2004 - Middle-Tier Hosting: Enterprise Services, IIS, DCOM, Web Services, and Remoting
    There is broad-reaching debate about remoting, Web services, Enterprise Services, and DCOM. In short, it is a debate about the best technology to use when implementing client/server communication in .NET. Rocky Lhotka shares his thoughts on the issue while offering clear explanations of basic application architecture terminology.
    [Read This Article]  [Top]
    Jul 21, 2004 - COM Interop Exposed
    This article provides and excellent foundation for COM Interop. It reviews COM's background, explains how VB6 interacts with COM, and then shows how to design .NET components to smoothly interact with COM.
    [Read This Article]  [Top]
    Jun 24, 2004 - The Perfect Service - Part 1
    The first article in this two-part series shows how to get Ambrose Little's .NET Service Manager running and then how to add plug-n-play services to it using drag-n-drop or XCOPY.
    [Read This Article]  [Top]
    May 25, 2004 - Generics In-Depth
    Although generics are extremely useful, they also seem to have a certain mystique that cannot be readily explained. This article hopes to remove that aura of mystery by showing just how easy it is to use generics and how useful they can be in many common situations.
    [Read This Article]  [Top]
    May 11, 2004 - SharePoint Security and .NET Impersonation
    When implementing custom components that require access to restricted resources, implicit impersonation must be used. Jay Nathan shows how to create a class that makes using .NET Impersonation a snap.
    [Read This Article]  [Top]
    Mar 23, 2004 - Exploiting .NET's Advanced Deployment Features
    Tony Arslan shows how to use VS .NET's custom deployment feature to create configuration files on the target machine during installation.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers