Sheenu Mehra
Sheenu Mehra

Reputation: 178

Find out how many users are online in asp.net web application

i am developing a web application in which students give their exams. Here is requirement to show which students are logged in in admin and faculty panel. First i have update a column in database named flag which turns to 1 on logged in time and on logged out turns to 0. But when browser forcefully closed it can not be updated. for this, i have used java script method, which fires whenever we navigate to another page and shows the student logged out.

Is there any solution for the same.

Upvotes: 1

Views: 2650

Answers (2)

Ajay
Ajay

Reputation: 6590

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)
{ 
 //Put user code to initialize the page here
 this.lblUserCount.Text = "Users online " + Application["UserCount"].ToString();
}

Or

If you use the built-in ASP.NET membership provider, then there's the ever-so-handy Membership.GetNumberOfUsersOnline() method.

Upvotes: 1

Ovidiu
Ovidiu

Reputation: 1407

There is no easy way to update your user's status as soon as he closed the browser without logging out. You could use the Session_End event in Global.asax, but that will be triggered when the session expires (usually 20 minutes). Also, in Session_End you have no information about the authenticated user, only the session id.

If you are using ASP.NET Membership to authenticate your users, there is a method that does what you need

Membership.GetNumberOfUsersOnline()

Upvotes: 1

Related Questions