-->

Monday, May 19, 2014

Count number of visitor on website in asp.net c#

Count number of visitor on website in asp.net c#

Introduction

If you want to count visitors, which is appear on your website. You can use Global.asax file for counting visitors. Google analytical tool is the best tool to count numbers of visitors, who is visit the site. I have simple demonstration of google analytical is:


  Now, create the simple code into c# for counting visitors.
Step-1 : Add Global.asax file into the project.
Step-2 : Add this code into  Application_Start block.

Application["visitcount"] = 0;

// In this code we create a application object, which hold 0. When application start by the server , This method is run once at a time.
Step-3 : Also add this code into Session_Start block.
 // Code that runs when a new session is started
        Application.Lock();
        Application["visitcount"] = (int)Application["visitcount"] + 1;
        Application.UnLock();
//  This method is raise on every post back event. The first line of code that is Application.Lock() define, only one client can access or modify the variable, which is stored in application object. The second line of code that is (Application["visitcount"] = (int)Application["visitcount"] + 1;) define, a application object incremented by one. In third line, locked application object will be unlocked.
Step-4 : Add new webform into the project, also take a label control on it.
Step-5 : On page_load event take the application object value onto the label text.

Complete Source code

<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
Code Behind
 protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Application["visitcount"].ToString();
    }
Global.asax file code
<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        Application["visitcount"] = 0;

    }
    
    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
        Application.Lock();
        Application["visitcount"] = (int)Application["visitcount"] + 1;
        Application.UnLock();

    }

    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.

    }
       
</script>
Now, run the application. Code generate the following output
Count number of visitor on website in asp.net c#

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved