You can use, Global.asax file's event to check who is online. You need to create Application variable and store array of user's Id or Name in Session_Start Event. and retrive it when you want it.
To remove the user from list, you need to handle Session_End event of global.asax. But this option will deal with all users how has hit your application.
If your application have login page and you want to show only those users who are log in to your system then you have to set Application Variable in your Login page's code behind.
1. In this page, set a variable LastVisitTime to store the last time the user visiting this page.
2. Use AJAX Timer, and every interval you can update database about the LastVisitTime of this user.
protected void Timer1_Tick(object sender, EventArgs e)
{
sql.update(update membertable set lastvisittime='" +DateTime.Now+ "' where username='" +User.Name+ "'");
}
3. Set the variable offVisitTime to check if this user is visiting the page.
DateTime lastvisit = Convert.ToDateTime(sql.select("select lastvisittime from membertable where username='" +User.Name+ "'"));
TimeSpan tSpanDifferent =DateTime.Now-lastvisit;
If(tSpanDifferent>SpanSpace)//SpanSpace is the variable which is the time span to chech if the user is off-visiting. And you can set it.
//The user is not visiting this page
For example, you can set Timer Interval as 2 minutes and set SpanSpace as 5 minutes.
4. After that, you can get the number of visiting users and who is visiting the page via looping.
Sub Application_OnStart
'initialize variable
Application("visitors_online") = 0
End Sub
Sub Application_OnEnd
End Sub
Sub Session_OnStart
Session.Timeout = 20 '20 minute timeout
Application.Lock
Application("visitors_online") = Application("visitors_online") + 1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors_online") = Application("visitors_online") - 1
Application.Unlock
End Sub
On line users: <%=Application("visitors_online")%>
Dim OnlineUsers As New MembershipUserCollection
For Each user As MembershipUser In Membership.GetAllUsers
If user.IsOnline Then
OnlineUsers.Add(user)
End If
Next
' here you can access the onlineUsers collection
gopalanmani
Star
7826 Points
1320 Posts
How can i get the "Who is Online"
Feb 01, 2008 05:56 PM|LINK
Hi All, How can i get the "Who is Online" list in asp.net 2.0/c# application. We are using "inproc" session mode.Thanks for Advance
Gopalan Mani
My Tech blog
tekhado
Member
438 Points
60 Posts
Re: How can i get the "Who is Online"
Feb 01, 2008 06:06 PM|LINK
hi -
You can use, Global.asax file's event to check who is online. You need to create Application variable and store array of user's Id or Name in Session_Start Event. and retrive it when you want it.
To remove the user from list, you need to handle Session_End event of global.asax. But this option will deal with all users how has hit your application.
If your application have login page and you want to show only those users who are log in to your system then you have to set Application Variable in your Login page's code behind.
Hardik Patel
(MCTS)
dharnendra
Contributor
2955 Points
551 Posts
Re: How can i get the "Who is Online"
Feb 02, 2008 03:50 AM|LINK
Hi, you can use HASH Table to store the online viewers details.
http://www.c-sharpcorner.com/UploadFile/munnamax/whoseonline08042007062609AM/whoseonline.aspx
Technical Leader
GTL-Ahmedabad
Punithkumar
Contributor
5192 Points
918 Posts
Re: How can i get the "Who is Online"
Feb 04, 2008 08:05 AM|LINK
1. In this page, set a variable LastVisitTime to store the last time the user visiting this page.
2. Use AJAX Timer, and every interval you can update database about the LastVisitTime of this user.
protected void Timer1_Tick(object sender, EventArgs e)
{
sql.update(update membertable set lastvisittime='" +DateTime.Now+ "' where username='" +User.Name+ "'");
}
3. Set the variable offVisitTime to check if this user is visiting the page.
DateTime lastvisit = Convert.ToDateTime(sql.select("select lastvisittime from membertable where username='" +User.Name+ "'"));
TimeSpan tSpanDifferent =DateTime.Now-lastvisit;
If(tSpanDifferent>SpanSpace)//SpanSpace is the variable which is the time span to chech if the user is off-visiting. And you can set it.
//The user is not visiting this page
For example, you can set Timer Interval as 2 minutes and set SpanSpace as 5 minutes.
4. After that, you can get the number of visiting users and who is visiting the page via looping.
Punithkumar
Punithkumar
Contributor
5192 Points
918 Posts
Re: How can i get the "Who is Online"
Feb 04, 2008 08:07 AM|LINK
To get number of users online..
Sub Application_OnStart
'initialize variable
Application("visitors_online") = 0
End Sub
Sub Application_OnEnd
End Sub
Sub Session_OnStart
Session.Timeout = 20 '20 minute timeout
Application.Lock
Application("visitors_online") = Application("visitors_online") + 1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors_online") = Application("visitors_online") - 1
Application.Unlock
End Sub
On line users: <%=Application("visitors_online")%>
Punithkumar
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: How can i get the "Who is Online"
Feb 04, 2008 11:33 AM|LINK
if you are using the membership service,
use this code :
Dim OnlineUsers As New MembershipUserCollection For Each user As MembershipUser In Membership.GetAllUsers If user.IsOnline Then OnlineUsers.Add(user) End If Next ' here you can access the onlineUsers collectionsatender88
Member
16 Points
46 Posts
Re: How can i get the "Who is Online"
Feb 09, 2008 04:55 AM|LINK
I also want the same fuction. but I am using VB and created a own registration process in SQL Server 2005
sql cache dependency
Punithkumar
Contributor
5192 Points
918 Posts
Re: How can i get the "Who is Online"
Feb 11, 2008 05:22 AM|LINK
U can convert VB code to c# code using this link..(Vice versa)
http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx
Punithkumar
SuHlaing
Member
6 Points
14 Posts
Re: How can i get the "Who is Online"
Jan 11, 2011 07:08 AM|LINK
Hi ,
I also want to get the number of online active user. When I try to implement as you mentioned, I got the error like that.
"System.Web.HttpApplication.Application' is a 'property' but is used like a 'method"
I am quite new to asp.net so I don't know how to solve it. I would be much appreciate for any help.
Thanks
Su Hlaing
Miodrag Zivk...
Participant
838 Points
168 Posts
Re: How can i get the "Who is Online"
Jan 11, 2011 10:26 AM|LINK
In C# you should use:
Application("visitors_online")Cheers
Miodrag