Monday 9 December 2013

Count Number of Visitors in Website using Asp.net in C#

Follow these steps :1,
Open Visual Studio à Create New Website à Right click on Solution Explorer à Select Add New Item à Select Global Application Class file and click 

Step :2,

In Global.ascx page do these changes


void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["NoOfApplicationVisitors"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfApplicationVisitors"] = (int)Application["NoOfApplicationVisitors"] + 1;
Application.UnLock();
}

Step:3, in aspx page



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<b>No of Visits in your application:</b>
</td>
<td>
<asp:Label ID="lblUserCount" runat="server" ForeColor="Red" />
</td>
</tr>
</table>
</form>
</body>
</html>

Step:4, in aspx.cs  code behind page



protected void Page_Load(object sender, EventArgs e)
{
lblUserCount.Text = Application["NoOfApplicationVisitors"].ToString();
}

Now you can execute and check.