2

I want to have a list that contains online users in my web site i use this code in local host but it did't shows me any user. Can any body help me? where did i make mistake?

  MembershipUserCollection users;


        if (!IsPostBack)
        {
            // Bind users to ListBox.
            List<MembershipUser> onlineUsers = new List<MembershipUser>();
            foreach (MembershipUser user in Membership.GetAllUsers())
            {

                if (user.IsOnline)
                {
                    onlineUsers.Add(user);

                }

            }
            ListBox1.DataSource = users;
            ListBox1.DataBind();
Davide Piras
  • 43,984
  • 10
  • 98
  • 147

1 Answers1

1

Try changing this line:

ListBox1.DataSource = users;

To this:

ListBox1.DataSource = onlineUsers;

Also do not forget to set the DataKeyField and DataValueField properties on the ListBox1.

Luke Baughan
  • 4,658
  • 3
  • 31
  • 54
  • Thanks Funny mistake!! it works, but it shows me some offline users!! that's strange! –  Jan 08 '13 at 12:33
  • No problem, glad to help - the "offline" users issue might be session timeout related - i.e. even though the user has closed the browser the session has not yet timed out and therefore is still showing them as "online". Oh don't forget to mark this question as complete (and vote up my answer if you are able!). – Luke Baughan Jan 08 '13 at 12:47
  • How can can i remove this problem. Do you have any idea? –  Jan 08 '13 at 13:37
  • AFAIK there is no "clean" way to do this - search stack for "ASP.NET Force logout" and you'll get a mix of results some of which might be suitable. – Luke Baughan Jan 08 '13 at 14:10
  • Where do I write this if am using asp.net MVC? – DoIt Feb 07 '14 at 16:12
  • @Dayakar see this link for MVC style solution http://stackoverflow.com/questions/4774347/get-a-list-of-online-users-in-asp-net-mvc – Luke Baughan Feb 11 '14 at 15:42