How to Count number of visitors visit to website/page in asp.net | ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

How to Count number of visitors visit to website/page in asp.net

Introduction: In this article I will try to explain how we can count the visitors visit the website/pages in asp.net.

Description:
Add a Global.asax file to project. You see the file as mention in attached snapshot:


Now write the below given code in Global.asax file:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.SessionState" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Collections" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup  
    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
     
    }


    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        UserVisitCount();
        DataSet ds = new DataSet();
        try
        {
            ds.ReadXml(Server.MapPath("~/CountVisitor.xml"));
        }
        catch (System.IO.FileNotFoundException)
        {
            throw;
        }
        catch
        {
            //Don't throw
        }

        //Session
        Session["hits"] = ds.Tables[0].Rows[0]["hits"].ToString();

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.
      
    }
    private void UserVisitCount()
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath("~/CountVisitor.xml"));
                int hits = Int32.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
                hits += 1;
                ds.Tables[0].Rows[0]["hits"] = hits.ToString();
                ds.WriteXml(Server.MapPath("~/CountVisitor.xml"));
            }
            catch (System.IO.FileNotFoundException)
            {
                throw;
            }
            catch
            {
                //Don't throw
            }
        }
         
</script>

Add a XML file to project. Here I add XML and keep its name CountVisitor.xml and add below mention code:

<counter>
  <count>
    <hits>0</hits>
  </count>
</counter>

After that now go to Page that you want to Track for Pageviews. Here I add a Label control to display pageviews. Now go to .aspx.cs page write the below mention code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblpage.Text = "Visitor Count= "+ Convert.ToInt32(Session["hits"].ToString());
        }
    }


In VB (.aspx.vb)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
  lblpage.Text = "Visitor Count= " & Convert.ToInt32(Session("hits").ToString())
        End If
    End Sub


Now build, run the project and check the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget