Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have to display the count of logging users in home page after login, that should be display for all users.like Total login users are: 1 / 2/.. based on users count.

I have used Application["UserCount"] variable and test in different browsers.
when i login that has display count perfectly but when i logout that have been maintaining in Application variable.


Example:

Login

logging users 1

in another browser
login

logging users 2

in another browser
login

logging users 3

after that logout final one

but the application is maintaing previous value and display logging users 4


so i need to decrease the logging users when i logout

Here is my code:
C#
public static int count = 0;  //for count of visitors

public void Application_Start(Object sender, EventArgs e)
{
    Application["myCount"] = count;
    AreaRegistration.RegisterAllAreas();
        
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
}

public void Application_End(Object sender, EventArgs e)
{
    Application["myCount"] = Application["myCount"];
}

public void Session_Start(Object sender, EventArgs e)
{
    count = Convert.ToInt32(Application["myCount"]);
    Application["myCount"] = count + 1;
}

public void Session_End(Object sender, EventArgs e)
{
    if (Session["PNetUserName"]!=null)
    {
        Session.Abandon();
        Session.Clear();
        Application["myCount"] = count - 1;
        count = Convert.ToInt32(Application["myCount"]);
    }
}


Please help how to resolve this.

Thank you.
Posted
Updated 30-Aug-13 6:04am
v3

1 solution

You can simplfy the setting of your count variable by using it as a property, which I believe will also solve your problem.

C#
public static int Count
{
    get
    { 
        return Application["myCount"] != null
                       ? Convert.ToInt32(Application["myCount"] 
                       : 0;
    }
    set { Application["myCount"] = value; }
}


Then simply use the short hand operators for incrementing and decrementing the counter:
C#
public void Session_Start(Object sender, EventArgs e)
{
    Count++;
}

public void Session_End(Object sender, EventArgs e)
{
    if (Session["PNetUserName"]!=null)
    {
        Session.Abandon();
        Session.Clear();
        Count--;
    }
}
 
Share this answer
 
v2
Comments
LazyTarget 20-Sep-13 11:57am    
Did it solve your problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900