XML Counter 3
Add Comment<div align="center"> <table border="0" width="90%" class="outline"> <tr> <td width="50%" class="outline"><b>Download File</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"><a href="../../file/counter3.zip" class="wbox">counter3.zip</a> (6kb)</td> <td width="50%" class="outline">Beta2</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> Page Counter - Every web master wants to know how many people and from where are visiting their pages (including Me) . A component without which any website is incomplete is a good Counter which helps the Web Developer to track the viewers and the viewers trend.<br> To meet this requirement I have developed a Counter in ASP.NET, since I have access only to free resources in have used XML as my database to store the visitor information. Although I am fully aware that if your site has high visitor count XML won't be a good database since the file size of the database file will run into Megabytes in the span of few hours. But you will find this example very useful to learn XML interaction in C#. This example has been updated for .NET SDK beta2<br> <br> <span class=wboxheado>Explanation</span><br> This example consists of 2 pages.<br> 1) Counter.aspx - Contains the code to get the Viewer information and post it in a XML file. <br> 2) viewcounter.aspx - Is a Administrator file. </p> <p align="justify"><span class=wboxheado>Requirements</span><br> 1) .NET SDK beta2 (Note: This example might not run on future versions of the SDK).<br> 2) ASP.NET hosting with appropriate permissions.</p> <p align="justify"><b>Xml Schema </b></p> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"><pre><Visitors> <xsd:schema id="Visitors" targetNamespace="" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:element name="Visitors" msdata:IsDataSet="true"> <xsd:complexType> <xsd:choice maxOccurs="unbounded"> <xsd:element name="Viewer"> <xsd:complexType> <xsd:sequence> <xsd:element name="UserAgent" type="xsd:string" minOccurs="0" /> <xsd:element name="UserHostAddress" type="xsd:string" minOccurs="0"/> <xsd:element name="UserHostName" type="xsd:string" minOccurs="0" /> <xsd:element name="BrowserType" type="xsd:string" minOccurs="0" /> <xsd:element name="BrowserName" type="xsd:string" minOccurs="0" /> <xsd:element name="MajorVersion" type="xsd:string" minOccurs="0" /> <xsd:element name="MinorVersion" type="xsd:string" minOccurs="0" /> <xsd:element name="Platform" type="xsd:string" minOccurs="0" /> <xsd:element name="Date" type="xsd:string" minOccurs="0" /> <xsd:element name="Time" type="xsd:string" minOccurs="0" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema> </Visitors></pre></td> </tr> </table> <p align="justify"><span class=wboxheado>Code</span><br> 1) <i><b>counter.aspx</b> :- The Counter Page </i></p> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre><%@ page language="c#" EnableSessionState="True" %> <span class=cmt><%-- These are the imported namespaces need to run the counter --%></span> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <html> <head> <title>Saurabh's XML Counter Script</title> <script language="C#" runat="server"> <span class=cmt>//script is called when the page is loaded</span> public void Page_Load(Object src, 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="db/xmlcounter.xml" ; if(!Page.IsPostBack){ <span class=cmt>//try-catch block containing the counter code</span> try { DataSet counterData = new DataSet(); <span class=cmt>//It is very Important to specify the "FileShare.ReadWrite" option. //This allows other viewers to also read and write to the Database</span> FileStream dataIn ; dataIn = new FileStream(Server.MapPath(dataFile), FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ; counterData.ReadXml(dataIn); dataIn.Close(); countMsg.Text+=counterData.Tables[0].Rows.Count.ToString(); <span class=cmt>//Update the Database only if a new session is there </span> if(Session["counter"]==null) { <span class=cmt>//Create a new DataRow from the DataSet schema</span> DataRow newRow = counterData.Tables[0].NewRow(); <span class=cmt>//Fill the columns of the DataRow</span> newRow["UserAgent"]=Request.UserAgent ; newRow["UserHostAddress"]=Request.UserHostAddress ; newRow["UserHostName"]=Request.UserHostName; HttpBrowserCapabilities bc = Request.Browser; newRow["BrowserType"]=bc.Type; newRow["BrowserName"]=bc.Browser; newRow["MajorVersion"]=bc.MajorVersion.ToString() ; newRow["MinorVersion"]=bc.MinorVersion.ToString(); newRow["Platform"]=bc.Platform; DateTime now = DateTime.Now ; newRow["Date"]=now.ToShortDateString(); newRow["Time"]=now.ToShortTimeString(); counterData.Tables[0].Rows.Add(newRow); FileStream dataOut ; <span class=cmt>//Save the Updated file to disk!</span> dataOut = new FileStream(Server.MapPath(dataFile), FileMode.Open, FileAccess.Write, FileShare.ReadWrite) ; counterData.WriteXml(dataOut, XmlWriteMode.WriteSchema); dataOut.Close(); <span class=cmt>//Set a Session variable</span> Session["counter"]="Set" ; } } catch(Exception edd) { <span class=cmt>//catch other exceptions</span> Response.Write("<font color=#FF0000>An Exception Occurred " +edd.ToString()+"</font>") ; } } } </script> </head> <body > <h3 align="center">Welcome to Saurabh's Counter Script</h3> <br> <p align="center"> This is a sample page which has the counter scripting in it. Take the script from this page and paste it on your page. </p> <asp:Label text="You are visitor No: " style="font-size:10pt" id="countMsg" runat="server" /> <br> <br> <h5>This counter script has been taken from <a href="http://www.MasterCSharp.com" >http://www.MasterCSharp.com </a> you can get more such free scripts too. <br> Counter Script made by <a href="mailto:saurabh@MasterCSharp.com">Saurabh Nandu </a> </h5> </body></html></pre> </td> </tr> </table><P> </P> <P>2)<I> <b> viewcounter.aspx</b> : The counter information viewing page</I></P> <table border="0" width="100%" class="code" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <pre><%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Data" %> <%@ Page Language="C#" %> <html> <head> <title>Saurabh's XML Counter Script</title> <script language="C#" runat=server> <span class=cmt>//this script is execute 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="db/xmlcounter.xml" ; try { Dataset counterData = new DataSet(); <span class=cmt>//Open a FileStream to the Database //"FileShare.ReadWrite" enables other user to also read and write to the file</span> FileStream fin ; fin = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); <span class=cmt>// Infer the DataSet schema from the XML data and load the XML Data</span> counterData.ReadXml(new StreamReader(fin)); <span class=cmt>//Close the stream</span> fin.Close(); <span class=cmt>//get the total no of viewers by getting the count of the no of //rows present in the Table </span> showtotal.Text ="Total Viewers :"+ counterData.Tables[0].Rows.Count.ToString() ; <span class=cmt>//databind the Repeater to the Dataset of table '0' ie the 'Viewer'</span> MyDataList.DataSource = counterData.Tables[0].DefaultView; MyDataList.DataBind(); } catch (Exception ed) { <span class=cmt>//if there is any exception then display it </span> Response.Write("<font color=#FF0000>An Exception occurred " +ed.ToString()+"</font>") ; } } </script> </head> <body > <h4>Welcome to Saurabh's XML Counter Viewing Page.</h4> <asp:label id="showtotal" text="" runat="server" /> <br> <ASP:Repeater id="MyDataList" runat="server"> <headertemplate> <h5> Viewer Details </h5> </headertemplate> <itemtemplate> <br> <table class="mainheads" width="60%" style="font: 8pt verdana" > <tr style="background-color:#FFFFCC"> <td>User Agent :</td> <td> <%# DataBinder.Eval(Container.DataItem, "UserAgent") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>User Host Address :</td> <td> <%# DataBinder.Eval(Container.DataItem, "UserHostAddress") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>User Host Name :</td> <td> <%# DataBinder.Eval(Container.DataItem, "UserHostName") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>Browser Type :</td> <td> <%# DataBinder.Eval(Container.DataItem, "BrowserType") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>Browser Name :</td> <td> <%# DataBinder.Eval(Container.DataItem, "BrowserName") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>Major Version :</td> <td> <%# DataBinder.Eval(Container.DataItem, "MajorVersion") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>Minor Version :</td> <td> <%# DataBinder.Eval(Container.DataItem, "MinorVersion") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>Platform :</td> <td> <%# DataBinder.Eval(Container.DataItem, "Platform") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>Date :</td> <td> <%# DataBinder.Eval(Container.DataItem, "Date") %> </td></tr> <tr style="background-color:#FFFFCC"> <td>Time :</td> <td> <%# DataBinder.Eval(Container.DataItem, "Time") %> </td> </tr> </table><br> </itemtemplate> </ASP:Repeater> <br> <h5>This counter script has been taken from <a href="http://www.MasterCSharp.com" > http://www.MasterCSharp.com</a> you can get more such free scripts too. <br> Counter Script made by <a href="mailto:saurabh@MasterCSharp.com">Saurabh Nandu </a> </h5> </body></html></pre> </td> </tr> </table>