Get the number of online users on website in asp.net

Ranjeet_8
Posted by Ranjeet_8 under ASP.NET category on | Points: 40 | Views : 2498
# Count no. of active users on website :
Add this code on your Global.asax Page

public void Session_Start(object sender, EventArgs e)
{
//Fires when the session is started.
Application["UserCount"] = Convert.ToInt32(Application["UserCount"].ToString()) + 1;
}
public void Session_End(object sender, EventArgs e)
{
//Fires when the session ends.
Application["UserCount"] = Convert.ToInt32(Application["UserCount"].ToString()) - 1;
}

Add this code on your Master Page

private void Page_Load(System.Object sender, System.EventArgs e)
{
lblUserCount.Text = "Users online " + Application["UserCount"].ToString();
}

Comments or Responses

Posted by: Sahoomanoj27 on: 10/30/2012 Level:Starter | Status: [Member] | Points: 10

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

}

Add this code on your Master Page source code

<div>

<asp:Label ID="lblUserCount" runat="server" Text="Label"></asp:Label>

<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>
</div>


Add this code on your Master Page code Behind

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




Login to post response