Working with Membership API

In this blog post, i will show you how to get specific user information from the database and bind it to your control.

1- Create a DataTable object with specific Column names.

2- Get all users and save them in the MembershipUserCollection

3- Use the Profilecommon class to retrieve specific user information.

4- Create Datarow for each user in the MembershipUserCollection with the needed information

5- Bind the DataTable to the DataView.

                // Create a datatable  with specific column names
                DataTable dt = new DataTable();
                dt.Columns.Add("Username", typeof(string));
                dt.Columns.Add("First name", typeof(string));
                dt.Columns.Add("Last name", typeof(string));
                dt.Columns.Add("City", typeof(string));
                dt.Columns.Add("Country", typeof(string));
          
            // Get all users in form of MembershipUserCollection
            MembershipUserCollection users = Membership.GetAllUsers();

            // Create a new ProfileCommon object
            ProfileCommon prof;

            // Instantiate a Datarow
            DataRow r;

            // Loop through all users inside the MembershipUser Collection, Retrieve the needed information, Add the Datarow to the Datatable.
            foreach (MembershipUser onlineUser in users)
            {
                r = dt.NewRow();
                prof = Profile.GetProfile(onlineUser.UserName);
                r["UserName"] = onlineUser.UserName;
                r["First name"] = prof.ProfileInfo.FirstName;
                r["Last name"] = prof.ProfileInfo.LastName;
                r["City"] = prof.ProfileInfo.City;
                r["Country"] = prof.ProfileInfo.Country;
                dt.Rows.Add(r);
            }

            // Set the datasource of the gridview to the datatable and bind it
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Page.DataBind();

 Best Regards,

HC

Comments

# re: Working with Membership API

Saturday, March 10, 2007 9:28 PM by caren

hi webmaster,

thanks for this tip.

# re: Working with Membership API

Monday, March 12, 2007 8:50 PM by mosessaur

very cool idea

# re: Working with Membership API

Friday, March 16, 2007 5:30 PM by patel

good one bro.. i liked it...

# re: Working with Membership API

Monday, April 16, 2007 8:05 AM by rany aof

hi

it is a good idea . i am plannig to read your articles at aspalliance ,

i saved your one about rich text editor and want to read it next few days .

if you have more articles i will happy to get the links of it throgh the mail

ranyaof@gmail.com  

thank you

Rany Aof

pisga-sys Xfactory Developer

Akko  Or in arabic 3akka (falasten almo7tala)

# re: Working with Membership API

Tuesday, April 17, 2007 3:12 AM by haissam

Thank you rany

It's very pleasant to me to hear such comments...

Best Regards,

HC

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.