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();
GridView1.DataBind();

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;
            dt.Rows.Add(dr);
        }
        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”
TypeName=”GetAllUsers”></asp:ObjectDataSource>
<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.

Advertisements

20 thoughts on “Membership.GetAllUsers() with Paging and Sorting

  1. 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. 🙂

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

  3. 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. 🙂

  4. reason you have the typename issue is probably because you don’t have that as a type – download the source code and put the getallusers.cs file in your app_code folder.

  5. thanks alot.
    was trying to use Linq to force MembershipUser into datatable but had trouble converting.
    now that its in a dataset, can easily manipulate it with Linq

  6. I am getting this error:
    The type specified in the TypeName property of ObjectDataSource ‘ObjectDataSource1’ could not be found.

  7. The reason your getting the TypeName error is that you must use the fully qualified class name of the class where your CustomGetAllUsers method exists.

    for example I placed mine directly in the code behind file of my .aspx page
    here the first few line that define the class (in the code behind)
    namespace onlinesubmission
    {
    public partial class ManageUsers : System.Web.UI.Page
    ….

    so my TypeName is onlinesubmission.ManageUsers

    the fully qualified name (namespace.classname)

  8. This helped a lot, thanks.

    Is it possible to add a view detail/edit user link in a column that passes the ProviderUserKey to an edit page? e.g. edituser.aspx?uid=xxxx-xxxx-xxxx-xxxx

    Cheers.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s