German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Create latest Users list for your website using ASP .NET 2.0

Written by omerkamal on Jan 16, 2007
How to create Latest members list using ASP .NET 2.0

Explanation:

In this tutorial we will discuss about creating a latest registred users/members list in a website.
 
We are going to perform this step by step:
  1. Add a Class GetUsersDatato your Folder App_Data this is where all non-compiled code goes.
  2. Add a new Class GetAllUsersDataTable() to it.
  3. Make it a static Class. This will make us easy to access the function. To learn more about static classes and static functions read our other article Static Classes and Static Class Members.
  4. Now our Function look like this;
 
Public static DataView GetAllUsersDataTable()
    {
        MembershipUserCollection mc = new MembershipUserCollection();
        mc = Membership.GetAllUsers();
 
        DataTable dt = new DataTable("LastUsers");
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Rdate", typeof(DateTime));
        string name = string.Empty;
        foreach (MembershipUser st in mc)
        {
            name = st.UserName;
            dt.Rows.Add(new Object[] { name, st.CreationDate });
        }
        dt.AcceptChanges();
        DataView dv = new DataView(dt);
        return dv;
    }
 
You noticed that 1st we defined a DataTable and later after filling it we changed it to a DataView. This is because DataView will give us better controle over Data Operations rather then a DataTable.
 
Now save your work in the new created Class and go the Page (.aspx) where actually you want Display the Lates users List.
Add a Table Control to the Page. It would be better idea to create a new DataPlaceHolder if you are using a Mater Page.
 
Name the Table “LatestUsersTable”. Don’t add any thing to the Table. Your Table will look like this now;
 
<asp:Table ID="LatestUsersTable" runat="server">
asp:Table>
 
Now, add the following code to your Page_Load event.
 
        DataView tView = GetUsersData.GetAllUsersDataTable();
        tView.Sort = "Rdate DESC";
 
This will sort the DataView Desecendingly on users Registration Date. By default the date enterd was in accending.
 
Now, it depends how many user you want to show in the latest user list. We are showing last 5 members. We enterd the upper limit 5 in or For Loop.
 
        for (int i = 0; i < 5; i++)
        {
            TableRow Row = new TableRow();
            TableCell C1 = new TableCell();
 
            C1.Text = @" +
              tView[i]["UserName"].ToString() + ">" +
 
            Row.Cells.Add(C1);
            LatestUsersTable.Rows.Add(Row);
        }
 
        TableRow Row4 = new TableRow();
        TableCell C4 = new TableCell();
        C4.Text = @"< All Members >";
        Row4.Cells.Add(C4);
        LatestUsersTable.Rows.Add(Row4);
 
 
In the Loop at first a row created, after that a cell is cerated for each user and evantually it is added to the current row of our Table. The actual trick lies in “tView[i]["UserName"]”. This way we are getting the 1st five rows of our DataView.
 
When the loop is finished we added one more row at the bottom of the Table, so when a user want to see the complete list of users he/she will just click on it and it will navigate them to our “Members/Users.aspx” Page.
Visitors/Readers Comments
(for questions please use The Forum)



123Stop End

I keep getting GetUsersData isnt declared

12/03/2007 23:14:23 UTC

Omer kamal
Did you create a new Class name GetUsersData in the App_Code Folder?

13/03/2007 00:58:48 UTC

123Stop End
Yes, Its the GetUSerdata.cs right?

14/03/2007 16:06:19 UTC

Omer Kamal
Be careful with the spellings. Names of the Classes are case sensitive. Its GetUsersData.cs instead.

14/03/2007 17:05:19 UTC

123Stop End
Is there supposed to be anything like Namespace or imports in the .cs?

17/03/2007 09:36:36 UTC

Omer Kamal
In C# code file (with file extension .cs)  we use "using" whereas in VB (with file extension .vb)  "import" is used to import a Name Space.

17/03/2007 10:04:00 UTC

123Stop End
The Problem ls because im using VB. ill try look around for a VB tutorial on this. Thanks for everything anyway!

18/03/2007 17:04:06 UTC

123Stop End
Wait a second, i got it working, i hadnt placed a "Inherit" thing.
Thank youuuu!

19/03/2007 00:42:10 UTC

123Stop End
Hey i dont know if you can help me on this one, When finding users online now, can i use the same procedure?

19/03/2007 01:36:50 UTC

Omer Kamal

There is already a Tutorial added in our web site.

https://dotnet-friends.com/tutorials/asp/tutinaspbb89d3f9-1bef-43a9-99a3-39f25c6a98ce.aspx

just add one more condition to the each if statment for checking that if the user is also online.

 if (usr.UserName.Contains(keyTosearch) && usr.IsOnline)
{
    // add it to the DataTable
}

P.S: please ask questions in the Forum this will help others also

19/03/2007 03:48:31 UTC

123Stop End
I have registerd but cant sign in for some reason. hmmm!

19/03/2007 08:01:17 UTC

kamal
Did u get the email containing your login data? Our web host is having problem with email server.

19/03/2007 08:45:47 UTC

132Stop End
Thats the problem. When i signed, it didnt show anywhere were i could input my password. ill resign. And i did check email but nothing, yet.

19/03/2007 13:29:00 UTC

kamal
Ok, I have sent you an email earlier. Do u have the login data now?

19/03/2007 15:31:07 UTC

123Stop End

Hi, No i havnt did u send it to the brio47 email? ill keep checking tho.

 

20/03/2007 07:10:57 UTC

123Stop End

Hi, No i havnt did u send it to the brio47 email? ill keep checking.

 

20/03/2007 07:13:52 UTC

kamal
yes to brio47. please, also check your Junk mails.
Your Question is moved to the related Tutorial page.

21/03/2007 03:26:13 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)