Friday 28 March 2014

how to count number of online users in asp.net

Simple two steps:

1.Add this code in Global.asax


void Application_Start(Object Sender, EventArgs E)
        {
            Application["CurrentUsers"] = 0;
        }
        void Session_OnStart(object Sender, EventArgs E)
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
            Application.UnLock();
        }
        void Session_OnEnd(object Sender, EventArgs E)
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
            Application.UnLock();
        }

2.In Aspx Page

<%=Application["CurrentUsers"].ToString()%>

In web.config timeout will be make 10, by default 20

No comments:

Post a Comment