Wednesday, 10 August 2016

Check User total no.of visits to the website using application state in

In this article I’ll show you how to make Users total no. of visits to any website with the help of application state in using c#, we also use global.asax file here.

Global.asax file is the application file that contains the code for application state level events raised by the or http modules. Global.asax is resides in the root directory of



Step1) Open Your Visual Studio 2010 and Create an Empty Website, Give a suitable name [Applicationstate_demo].

Step2) In Solution Explorer you get your empty website, then add a Web Form and Global.asax file. By going like this –

For Web Form:
applicationstate_demo (Your Empty Website) -> Right Click -> Add New Item -> Web Form. Name it as -> applicationstate.aspx.

For Global.asax:
applicationstate_demo (Your Empty Website) -> Right Click -> Add New Item - -> Global Application Class.

C:\Users\Nilesh\Desktop\global.asax - Copy.jpg

Global.asax Code:

<%@ Application Language="C#" %>

<script runat="server">

   void Application_Start(object sender, EventArgs e)
       // Code that runs on application startup
       Application["VisitorCount"] = 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["VisitorCount"] = (int)Application["VisitorCount"] + 1;

   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.


As Application start we have visitor count = 0 ,as we go ahead we increment the visitor count to +1.


As the session start we make the visitor count to +1 in the event of session start.


This is my welcome page you can make your own or you can just make it blank and print the label for total visiting.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
   <style type="text/css">
           text-align: center;
           font-size: xx-large;
<body bgcolor="#ffffcc">
   <form id="form1" runat="server">
   <div class="style1">
       <strong><span class="style2">Welcome to Active DotNet<br />
       <br />
       </span></strong><br />
       <br />
       <br />
       <br />
       <asp:Image ID="Image1" runat="server" Height="350px" ImageUrl="~/1.jpg" />
       <br />
       <br />
          <asp:Label ID="Label2" runat="server"
           style="text-align: center; font-weight: 700; font-size: large"></asp:Label>


Here in the label we will print out the total visiting. Look below image.



Hope you like this. Have a nice day. Thank you for Reading.