XML Guestbook 3
Add Comment| Download File | SDK |
| guestbook3.zip (8kb) | Beta2 |
Introduction
Guestbook- is one of the component of a website which a web master loves to read
(Including Me!!!) . So here I have a guest book for everyone. It uses XML as its
Database because all servers support XML files. Also you can customize the
viewing of XML file as you like. This example has been updated for .NET SDK Beta2
Requirements
1) .NET SDK beta2 (Note: This example might not work on future
versions of the SDK).
2) ASP.NET supported web server.
Explanation
This example consists of 2 parts.
1) guestpost.aspx - Contains the code to post user information in a
XML file.
2) viewguestbook.aspx - A DataSet object is created, then it is very easy to manipulate
the Data to viewing purpose. In this example we use a
"Repeater" to display the data in the DataSet.
Code
1) guestpost.aspx :- The Guestbook post page
<%@ Page Language="C#" EnableSessionState="False" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <%-- These are the imported namespaces needed to run the guest book --%> <html> <head> <title>Welcome to Saurabh's GuestBook.</title> <script Language="C#" runat="server"> ///<summary> /// This method is called when the submit button is clicked ///</summary> public void Submit_Click(Object sender, EventArgs e) { //the path to the Xml file which will contain all the data //modify this if you have any other file or directory mappings. string dataFile = "db/guest.xml" ; //put the posting code within a Try-Catch block try{ //proceed only if the page is Valid if(Page.IsValid){ errmess.Text="" ; //Open a FileStream to the Database in read mode FileStream fin; fin= new FileStream(Server.MapPath(dataFile),FileMode.Open, FileAccess.Read,FileShare.ReadWrite); //Create a DataSet object DataSet guestData = new DataSet(); //Read only the Xml Schema from the Database guestData.ReadXmlSchema(fin); fin.Close(); //Create a new DataRow from the DataSet Schema DataRow newRow = guestData.Tables[0].NewRow(); //Fill the DataRow with form values newRow["Name"]=Name.Text; newRow["Country"]=Country.Text; newRow["Email"]=Email.Text; newRow["Comments"]=Comments.Text; newRow["DateTime"]=DateTime.Now.ToString(); //Add the row to the DataSet guestData.Tables[0].Rows.Add(newRow); //Create another filestream to the DataBase file //in write mode and save the file! FileStream fout ; fout = new FileStream(Server.MapPath(dataFile),FileMode.Open, FileAccess.Write,FileShare.ReadWrite); guestData.WriteXml(fout, XmlWriteMode.WriteSchema); fout.Close(); //Hide the Form Panel formPanel.Visible=false; //Display the Thank Panel thankPanel.Visible=true; } } catch (Exception edd) { //catch any other exception that occur errmess.Text="Cannot write to XML file because "+edd.ToString() ; } } </script> <LINK href="mystyle.css" type=text/css rel=stylesheet> </head> <body > <%-- Include a header file 'header.inc' --%> <!-- #Include File="header.inc" --> <br> <h3 align="center" class="newsbody">Guestbook Post Page.</h3> <br> <asp:label id="errmess" text="" style="color:#FF0000" runat="server" /> <asp:Panel id=formPanel runat=server> <form runat="server"> <table border="0" width="80%" align="Center"> <tr > <td class="newsheading"><b>Sign-in My GuestBook</b></td> <td class="newsheading"> </td> </tr> <tr class="newsbody" > <td>Name :</td> <td ><asp:textbox text="" id="Name" runat="server" /> <asp:RequiredFieldValidator ControlToValidate=Name display=static runat=server> *</asp:RequiredFieldValidator></td></tr> <tr class="newsbody"><td>Country :</td> <td><asp:textbox text="" id="Country" runat="server"/> <asp:RequiredFieldValidator ControlToValidate=Country display=static runat=server> *</asp:RequiredFieldValidator></td> </tr> <tr class="newsbody"><td>E-Mail :</td> <td><asp:textbox test="" id="Email" runat="server"/> <asp:RequiredFieldValidator ControlToValidate=Email display=static runat=server> *</asp:RequiredFieldValidator><asp:RegularExpressionValidator runat="server" ControlToValidate="Email" ValidationExpression="[\w-]+@([\w-]+\.)+[\w-]+" Display="Static" Font-Name="verdana" Font-Size="10pt"> Please enter a valid e-mail address</asp:RegularExpressionValidator></td> </tr><tr class="newsbody"><td>Comments :</td> <td><asp:Textbox textmode=multiline id="Comments" columns="25" rows="4" runat="server" /></td></tr> <tr class="newsbody"> <td colspan="2" > <asp:Button class="newsheading" id="write" Text="Submit" onClick="Submit_Click" runat="server"/></td></tr></table></form></asp:Panel> <asp:Panel id=thankPanel visible=false runat=server> <p class="newsbody" align=center><b>Thank you for posting in my Guestbook!</b> <br><a href="viewguestbook.aspx">Click here </a> to view GuestBook. </p> </asp:Panel> <!-- #Include File="footer.inc" --> </body> </html> |
2) viewguestbook.aspx : The Guestbook viewing page.
<%@ Page Language="C#" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <%-- Needed Namespaces --%> <html> <head> <title>Welcome to Saurabh's GuestBook.</title> <script language="C#" runat=server> //run the script when the Page is Loaded public void Page_Load(Object sender, EventArgs e) { //the path to the Xml file which will contain all the data //modify this if you have any other file or directory mappings. string datafile = "db/guest.xml" ; //try-Catch block to read from an XML file try { //create a DataSet object DataSet guestData = new DataSet(); //Open a FileStream to the Database FileStream fin ; fin = new FileStream(Server.MapPath(datafile),FileMode.Open, FileAccess.Read,FileShare.ReadWrite) ; //Read the Database into the DataSet guestData.ReadXml(fin); fin.Close(); //Databind the first table in the Dataset to the Repeater MyDataList.DataSource = guestData.Tables[0].DefaultView; MyDataList.DataBind(); } catch (Exception edd) { //catch any other exceptions that occur errmess.Text="Cannot read from XML file because "+edd.ToString() ; } } </script> <LINK href="mystyle.css" type=text/css rel=stylesheet> </head> <body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" rightmargin="0"> <!-- #Include File="header.inc" --> <asp:label id="errmess" text="" style="color:#FF0000" runat="server" /> <br> <h3 align="center" class="newsbody">My Guestbook.</h3> <ASP:Repeater id="MyDataList" runat="server"> <headertemplate> <table class="mainheads" width="100%" style="font: 8pt verdana"> <tr style="background-color:#FF9966"> <th> Name </th> <th> Country </th> <th> Email </th> <th> Comments </th> <th> Date/Time </th> </tr> </headertemplate> <itemtemplate> <tr style="background-color:#FFFFCC"> <td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Country") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Email") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Comments") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "DateTime") %> </td> </tr> </itemtemplate> <footertemplate> </table> </footertemplate> </ASP:Repeater> <!-- #Include File="footer.inc" --> </body></html> |

