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