Hit Counter for ASP.NET using C#

Count Number of Visitors in WebSite using ASP.Net and C# :

Markup Code (Web User Control):

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

Code for the code behind:

C#

        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"));

        }

Then you need to have an xml file in the root directory to make the code work as well. The XML file will look like this:

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

</counter>

Refference:


Beginning ASP.NET E-Commerce in C#: From Novice to Professional (Expert’s Voice in .NET)

1. http://www.codeshode.com/2011/07/hit-counter-for-aspnet-using-c-vbnet.html

Database Driven Hit Counter:

http://imar.spaanjaars.com/238/howto-create-a-hit-counter-using-a-database-in-aspnet-1x-with-c-sharp

Online User (Using global.aspx):

http://aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s