WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 127 users


WorldofASP.NET >> ASP.NET >> Applications

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here

Building GuestBook using XML and ASP.NET

Sample code and tutorial on how to build your own simple guest book with the help of XML and no Database backend needed
Published Date : 09 Dec 2007
Author : James Douglas
Language : VB.NET,C#
Platform : .NET
Technology : None,Visual Studio,ASP.NET
Views : 7242
Rating : (3 votes so far)


Introduction

This article will basically guide you on how to create a simple GuestBook for your website with the help of XML. Lots of guestbook that you can found on  the internet basically utilise Database to store the backend data. In fact, for guestbook, you actually don't need to store all the data inside the database. You can just use XML file for storing the Data. XML is lightweight and you can format the look and feel easily using XSLT and you can also open it direct with Notepad. You don't have to worry about the connection string especially if you migrate your website to different hosting provider.

Building ASP.NET GuestBook with XML Backend

To Start building GuestBook for your website, lets open Visual Studio 2005 and Create two pages called
GuestBook.aspx, and ShowGuestBook.aspx.

The GuestBook.aspx will be a page where your user can fill in their data and submit to your website and ShowGuestBook.aspx will be a page that display current available guest on your website.
Both pages will be retrieving and saving the data into the XML File. No database needed.

Let's Start code something for GuestBook.aspx

 <TABLE WIDTH="600">
     <TR>
        <TD>
            Your Name</TD>
        <TD>
            <ASP:TEXTBOX ID="txtName" RUNAT="server" COLUMNS="30" MAXLENGTH="30" /></TD>
     </TR>
     <TR>
         <TD>
            Email Address</TD>
         <TD>
            <ASP:TEXTBOX ID="txtEmail" RUNAT="server" COLUMNS="50" MAXLENGTH="50" /></TD>
     </TR>
     <TR>
         <TD VALIGN="top">
             Your Comments</TD>
         <TD>
         <ASP:TEXTBOX ID="txtComments" RUNAT="server" COLUMNS="30" ROWS="4" TEXTMODE="multiline"
          WRAP="true" /></TD>
     </TR>
     <TR>
        <TD COLSPAN="2" ALIGN="right" style="height: 26px">
             <ASP:BUTTON ID="btnSubmit" TEXT="Submit" ONCLICK="btnSubmit_Click" RUNAT="server" /></TD>
     </TR>
 </TABLE>
 <BR />
 <A HREF='ShowGuestBook.aspx'>View GuestBook</A>
The Page should be displayed like this

For this page, we need to handle the Submit Button Click Event. And the OnClick Event basically will save the current data into XML File. For this, we will be using DataSet objects to manipulate and save the XML File. There is a method called WriteXML from the DataSet objects that we can use to achieve this functionality. There is also another subset classes from .NET Framework that specifically written for XML manipulation, and all the classes is inside the System.XML objects. However I choose to use DataSet objects because it could represent XML data as a relational information as if you are working on a real relational database.

Submit Button Code Behind (GuestBook.aspx)
protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (!File.Exists(Server.MapPath("XML/GuestBook.xml")))
    {
        CreateXMLFile();
    }
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("XML/GuestBook.xml"), XmlReadMode.ReadSchema);

    DataRow dr = ds.Tables[0].NewRow();
    dr["datetime"] = DateTime.Now;
    dr["name"] = txtName.Text.ToString();
    dr["email"] = txtEmail.Text.ToString();
    dr["comments"] = txtComments.Text.ToString();
    ds.Tables[0].Rows.Add(dr);

    //Write to XML
    ds.WriteXml(Server.MapPath("XML/GuestBook.xml"), XmlWriteMode.WriteSchema);

    txtName.Text = "";
    txtEmail.Text = "";
    txtComments.Text = "";
}

CreateXML Method
(GuestBook.aspx)
private void CreateXMLFile()
{
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("GuestBook");
    DataColumn dc = dt.Columns.Add("id", Type.GetType("System.Int32"));
    dc.AutoIncrement = true;
    dc.AutoIncrementSeed = 1;
    dc.AutoIncrementStep = 1;
    dt.Columns.Add("datetime", Type.GetType("System.DateTime"));
    dt.Columns.Add("name", Type.GetType("System.String"));
    dt.Columns.Add("email", Type.GetType("System.String"));
    dt.Columns.Add("comments", Type.GetType("System.String"));
    ds.WriteXml(Server.MapPath("XML/GuestBook.xml"), XmlWriteMode.WriteSchema);
}
As you can see from the code above, we just use the DataSet WriteXML Method to write to the XML Files.
We set the Id to be autoincrement, everytime the user sign the guestbook

Now, for the ShowGuestBook.aspx, the code is basically pretty simple. We use Repeater to display the data from the XML File.
Below is the code snippet

ShowGuestBook.aspx
<FORM ID="form1" RUNAT="server">
    <ASP:REPEATER ID="myRepeater" RUNAT="server">
        <HEADERTEMPLATE>
            <TABLE BORDER="0" CELLPADDING="2" CELLSPACING="0" WIDTH="100%" STYLE="font: 10pt verdana">
                <TR>
                    <TD><h2>My Guest Book</h2></TD>
                </TR>
        </HEADERTEMPLATE>
        <ITEMTEMPLATE>
            <TR>
                <TD WIDTH="100%">
                    Name :
                    <%# DataBinder.Eval(Container.DataItem, "name") %>
                    <BR />
                    Email : 
                        <%# DataBinder.Eval(Container.DataItem, "email") %>
                    <BR />
                    Comments :
                     <%# DataBinder.Eval(Container.DataItem, "comments") %>
                     <BR />
                     Date :
                       <%# DataBinder.Eval(Container.DataItem, "datetime") %>
                       <BR /><BR />
                       <HR />
                </TD>
            </TR>
        </ITEMTEMPLATE>
        <FOOTERTEMPLATE>
            </table>
        </FOOTERTEMPLATE>
    </ASP:REPEATER>
    </FORM>
Code for Binding the Repeater (ShowGuestBook.aspx)
private void BindData()
{
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("XML/GuestBook.xml"),XmlReadMode.ReadSchema);
    DataView dv = ds.Tables[0].DefaultView;
    dv.Sort = "id desc";
    myRepeater.DataSource = dv;
    myRepeater.DataBind();
}

Conclusion

That's all you need basically to create a simple GuestBook that writes and read from XML file. As our code is written using DataSet.  You can actually easily change the code so that it reads from Access Database or MSSQL Database. It is your choice. If you have large user base, it might be good idea to store it into database.

Download Source Code



Other Related and Popular Articles :

Sending Email in ASP.NET 2.0
Send email in ASP.NET 2.0 Framework with or without SMTP Authentication

URL Rewriting with ASP.NET 2.0
How to implement URL Rewriting and Improve your SEO rankings.

Building a Photo Tagging Application using ASP.NET 2.0, LINQ, and Atlas
In this article, I will examines how to build a photo tagging application using ASP.NET 2.0, LINQ and Atlas framework.

Using LINQ to XML (and how to build a custom RSS Feed Reader with it)
In this article, Scott examines how to work with LINQ using XML. He also demonstrates how to build a custom RSS Feed Reader using these technologies.

Creating Contact Us Form easily using ASP.NET and SMTP
This article explain how to create a simple contact us form by using ASP.NET and System.Net.Mail to send email to the website owner

Build a DotNetNuke FileManager in ASP.NET
Tutorial and Code Sample on building a DotNetNuke FileManager in ASP.NET


Author Profile : James Douglas

I work in a Software House Company in Malaysia (Kuala Lumpur) and I am MCP Certified in C# and Web Application course.
I originally started my programming in Java but later on changed to Microsoft platform because of the simplicity and ease of use.
I love .NET programming and am doing it almost every day now.

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

#
19 Dec 2007 1:22 by : Sudheer

Vry Nice article .....

Leave New Comments


Article Content copyright by James Douglas
Everything else Copyright © by WorldofASP.NET 2008





Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card