Friday, 7 June 2013

How to create HIT COUNTER in asp.net


How to create HIT COUNTER in asp.net 




Create XML page
<?xml version="1.0" encoding="utf-8" ?>
    <counter>
      <count>
         <hits>0</hits>
      </count>
   </counter>

Create UserControl and code usercontrol.cs page
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="counter.ascx.cs" Inherits="counter"%>
<asp:Label ID="lblCounter" runat="server"></asp:Label>




        protected void Page_Load(object sender, EventArgs e)
        {
            this.countMe();

            DataSet tmpDs = new DataSet();
            tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

            lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
        }

        private void countMe()
        {
            DataSet tmpDs = new DataSet();
            tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

            int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

            hits += 1;

            tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();

            tmpDs.WriteXml(Server.MapPath("~/counter.xml"));

        }

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home