I have below code to display "list of all the online users" inside listBox using Membership function.
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MembershipUserCollection allUser = Membership.GetAllUsers(); // to get all the users
List<MembershipUser> onlineUsers = new List<MembershipUser>(); //creating list of online users
foreach (MembershipUser user in allUser)
{
if (user.IsOnline)
{
onlineUsers.Add(user);
}
}
Lbl_NumOfOnline_User.Text = (Membership.GetNumberOfUsersOnline()).ToString(); //return no. of online user
lst_Users.DataSource = onlineUsers; //return online users
//lst_Users.DataSource = allUser; // return all the users
lst_Users.DataBind();
}
I want to remove that user from the list of online users, who himself has logged in.
Example: if I had logged-in with UserName: Admin, then this Admin should not show to me in the list of "online users"
Tried below Code but not working:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MembershipUserCollection allUser = Membership.GetAllUsers(); // to get all the users
List<MembershipUser> onlineUsers = new List<MembershipUser>(); //creating list of online users
MembershipUser m_user = Membership.GetUser(); //to get self logged-in user
Session["UName"] = m_user.UserName.ToString();
if (m_user != null)
{
onlineUsers.Remove(m_user);
}
foreach (MembershipUser user in allUser)
{
if (user.IsOnline)
{
onlineUsers.Add(user);
}
}
Lbl_NumOfOnline_User.Text = (Membership.GetNumberOfUsersOnline()).ToString(); //return no. of online user
lst_Users.DataSource = onlineUsers; //return online users
//lst_Users.DataSource = allUser; // return all the users
lst_Users.DataBind();
}