Membership.GetAllUsers() with Paging and Sorting

Yesterday, I launched the website of the Philippine .NET Users Group (PHINUG) coinciding with Microsoft Philippine’s TechFest. I was monitoring the growth of the membership of the site until I was met with an exception while sorting the GridView of memberlist.

Since the site uses the ASP.NET 2.0 Membership, I can take advantage of the method Membership.GetAllUsers() to get all the users of the site whether the user is activated or not.

GridView1.DataSource = Membership.GetAllUsers();

Unfortunately, if you call the method to bind it to a GridView, it will display all the MembershipUser information (unless you customized the columns of the gridview) but you won’t be able to Page or Sort it. When you try to page the GridView, it will call an exception:

The GridView ‘GridView1 fired event PageIndexChanging which wasn’t handled.

Which, by customizing, will bring us back to the old days of Paging where you need to specify the index (base count) and the number of rows needed to fetch.

An easier way to enable Membership.GetAllUsers() with paging and sorting when bound to a GridView is to store the collection into a DataSet. As a modification, I placed my customized method in my DAL so that I can use an ObjectDataSource to consume it and bind it to a databound control which in this case is a GridView.

    public DataSet CustomGetAllUsers()    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt = ds.Tables.Add("Users");

        MembershipUserCollection muc;
        muc = Membership.GetAllUsers();

        dt.Columns.Add("UserName", Type.GetType("System.String"));
        dt.Columns.Add("Email", Type.GetType("System.String"));
        dt.Columns.Add("CreationDate", Type.GetType("System.DateTime"));

        /* Here is the list of columns returned of the Membership.GetAllUsers() method
         * UserName, Email, PasswordQuestion, Comment, IsApproved
         * IsLockedOut, LastLockoutDate, CreationDate, LastLoginDate
         * LastActivityDate, LastPasswordChangedDate, IsOnline, ProviderName

        foreach(MembershipUser mu in muc) {
            DataRow dr;
            dr = dt.NewRow();
            dr["UserName"] = mu.UserName;
            dr["Email"] = mu.Email;
            dr["CreationDate"] = mu.CreationDate;
        return ds;

To consume this list, I used an ObjectDataSource control and selected the business object where I placed this method.

<asp:ObjectDataSource ID=”ObjectDataSource1″ runat=”server” SelectMethod=”CustomGetAllUsers”
<asp:GridView ID=”GridView1″ runat=”server” CellPadding=”4″ ForeColor=”#333333″ GridLines=”None”
Width=”100%” AllowPaging=”True” AllowSorting=”True” DataSourceID=”ObjectDataSource1″ />

You can try the said functionality by downloading the files here. Note however that you need to rename the file to .zip as WordPress doesn’t accept Zip files as attachment. Ensure also that your application points to an ASP.NET Membership database and has at least 10 records (default PageSize) to see the Paging functionality.


6 Responses to “Membership.GetAllUsers() with Paging and Sorting”

  1. 1 1001001100100 February 8, 2008 at 5:21 am

    Very Nice!

  2. 2 JustRun April 9, 2008 at 10:53 am

    Many Thanks, But would u provide me with paging and sorting?

  3. 3 Patrick April 9, 2008 at 11:47 am

    As mentioned in the blog post above, you just have to set the “AllowPaging” and “AllowSorting” property of your GridView. If you have specified “AutoGenerateColumns” to false, you need to set in your “Columns” section what specific columns would sorting happen and what would be the sort expression that you will be using. 🙂

  4. 5 hunzonian June 10, 2008 at 2:36 pm

    very nice. unfortunately i get an error: System.InvalidOperationException: The type specified in the TypeName property of ObjectDataSource ‘ObjectDataSource1’ could not be found.

  5. 6 Patrick June 10, 2008 at 2:53 pm

    Hello, you might be setting a wrong type for a datacolumn. Check your code against the column type of the aspnet_Membership table. From my observation, the Membership.GetAllUsers() gets the rows from this table.

    Hope this helps. We’re using this extensively on various applications. 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog Stats

  • 4,031 hits
January 2008
« Dec   Feb »

%d bloggers like this: