Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


XML Guestbook 4

Add Comment
 

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

Comments

Add Comment