You must Sign In to post a response.
  • Category: Career Guidance

    How to maintain the correct logged in users count in asp.net application

    Hi All,
    Today I met a interview question like "Consider a Asp.Net appreciation page, in the top of the page u have a label like Logged in users Count" So u need to show correct logged in users count.
    I replied like i will set a flag in userstable in Db and show the result based on that flag count when the page load.

    He said k ..If a user properly logged out means u will update the flag in userstable in Db,the question is if users close the browsers tab or the user close the browser means how u will update the users table flag.

    Finally he said the answer we can done by using global.asax application close event.

    Now I checked,I m having a sample applciation with global.asax file.After i run the application ,I close the tab and also tried with close the tab.But the "Application_End" event not firing.

    So how to solve that the above mentioned loggedin users count problem.....


    Thanks in Advance :)
  • #759593

    Hi,

    To maintain logged in users count in asp.net application, you need to use Global.asax events. Check the below events which can be used to managed logged in users count

    Application_Start()
    Session_Start()
    Session_End()
    Application_End()

    simple code
    <code>
    void Application_Start(object sender, EventArgs e)
    {
    Application["UsersCount"] = 0;
    }

    void Session_Start(object sender, EventArgs e)
    {
    if (Application["UsersCount"] != null)
    {
    Application.Lock();
    Application["UsersCount"] = ((int)Application["UsersCount"]) + 1;
    Application.UnLock();
    }
    }
    void Session_End(object sender, EventArgs e)
    {
    Application.Lock();
    Application["UsersCount"] = ((int)Application["UsersCount"]) - 1;
    Application.UnLock();
    }
    </code>

    To get the current logged in user, use this code:

    string loggedInUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

    if you're using membership you can do:
    string loggedInUser = Membership.GetUser()

    Check the below links for more information
    http://www.aspdotnet-suresh.com/2013/11/aspnet-count-number-of-online-users.html
    http://www.webcodeexpert.com/2013/10/how-to-count-number-of-times-website.html
    http://www.codeproject.com/Articles/29792/Online-active-users-counter-in-ASP-NET
    http://www.codeproject.com/Articles/87316/A-walkthrough-to-Application-State


    Thanks & Regards,
    Mahesh Durgam

  • #759596
    Hi,

    better u create a application variable in app_start() event in global.asax like:
    void Application_Start(object sender, EventArgs e)
    {
    Application["LoggedinUsersCount"] = 0;
    }

    same like in session_start(object sender, EventArgs e){

    count the user loggedin time , that means u increase the loggedin time by one one..
    }

    Hope u understnads..

    Thanks,
    Chitaranjan

  • #759601
    You can use UsersCount, GuestUsersCount, RegistredUsersCount, SetUserOffline and SetUserOnline methods to maintain correct logged in users count in asp.net application or you can use following code snippet in aspx

    <body>
    <form id="form1" runat="server">
    <table>
    <tr>
    <td>
    <b>Total Number of Online Users</b>
    </td>
    <td style="color:Red">
    <%=Application["OnlineVisitors"].ToString()%>
    </td>
    </tr>
    </table>
    </form>
    </body>

  • #759604
    Hi

    Use application state or session state to count the onlie users.
    By using inproc you can maintain till the browser is closed.

    Application state:
    1.The application state is maintained in the
    application level. It is common for all users in a
    application.
    2.Application variable can be set as
    Application[ "Countvariable" ] = Convert.ToInt
    32(Application[ "Countvariable" ]) + 1;
    lbl.text= Application[ "Countvariable" ].ToString();
    3.Some application events in global.asax are
    Application_start
    void Application_Start( object sender, EventArgs
    e)
    {
    Application[ "Countvariable" ] = 0;
    }

    Session state:
    1.Session state is maintained user level. It is
    different for different users.
    2.Session variable can be set as
    Session[ "Countvariable" ] = Convert.ToInt
    32(Session["Countvariable" ]) + 1;
    lbl.text=Session["Countvariable"].ToString();
    3.Some session events in global.asax are
    Session_Start
    void Session_Start( object sender, EventArgs e)
    {
    Session[ "Count" ] = 0;
    }

    Regards.

    Sridhar Thota.
    DNS Member.
    "Hope for the best.. Prepare for the worst.."

    Sridhar Thota.
    Editor: DNS Forum.

  • #759620
    Hello Ramesh Kumar,

    Refer the below code :

    protected void Application_Start(object sender, EventArgs e)
    {
    //Session Count is intialized with 0.
    Application["SessionCount"] = 0;
    }

    protected void Session_Start(object sender, EventArgs e)
    {
    Application.Lock();

    int countSession = (int)Application["SessionCount"];
    Application["SessionCount"] = countSession + 1;

    Application.UnLock();
    }

    protected void Session_End(object sender, EventArgs e)
    {
    Application.Lock();

    int countSession = (int)Application["SessionCount"];
    Application["SessionCount"] = countSession - 1;

    Application.UnLock();
    }


    Refer the below site for more example :

    http://www.webcodeexpert.com/2013/10/how-to-count-number-of-times-website.html


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #759687
    Hi Members,

    I am learner i start a small project called student info. I create a design and data base i want to insert a data . Would u please help me how to write a insert code to connect the Ms sql data base. I attach a project file in this forum.

    With regards
    blackcaps
    black_caps45@yahoo.in

    student-info.rar

    Delete Attachment

  • #759690
    Hello Blackcaps,

    Please post your question in different thread.
    This thread is created by someone else.

    Please don't create spam.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #759805
    Hai Ramesh,
    In the scenario you have given, you need to understand 2 things:
    1. The total count of the user will be maintained by the application event and when the user logged in to the web site will be maintained by the session event.
    2. Once the user has logged in, you need to increase the number and show using the Application event as it should be displayed to all the users whether they are logged in or not(same as DotNetSpider). Also when the user is logged out, you need to reduce the number and again show to the Application event.
    So all these scenario will be handled only by the Application and session events and nothing to do with the database here.
    In the Application_Start event, Set the count variable as 0 and increment when a session_Start gets some value. it means when a user logged in to the application.
    Same way reduce this variable value when the Session_End is getting called when a user is logged out.
    Hope it will give you enough hint to understand.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments