Jeremy Wadsworth
My Contribution to the ASP.NET Community


Welcome   
Welcome to my personal website. Here you will find blog posts related to web development as well as personal related interests. If you're here for the source code to my Personal Web Site Kit, please register and you'll receive an email when your account has been approved.


Latest Blog Entries

 Monday, September 18, 2006


Adding custom properties for membership users in ASP.NET 2.0    

This instruction is for the programmer that doesn’t really care to hear about the nuts and bolts of adding profile properties to membership users, but just wants the quick read on what to do. I’m assuming that you’re already using the ASP.NET 2.0 membership services.

Adding custom properties for users is really quite simple. In the web.config you define your properties in the <system.web> section like the following code.
   <profile>
      <properties>
         <add name="FirstName"/>
         <add name="LastName"/>
         <add name="Address1"/>
         <add name="Address2"/>
         <add name="City"/>
         <add name="State"/>
         <add name="Zip" />
         <add name="PhoneNumber"/>
      </properties>
   </profile>

Once you have defined your properties in the web.config, the properties magically become accessible through the ProfileCommon class. The ProfileCommon class is only available after adding the Profile section to the web.config.

Now that you have defined the additional properties for users, you can now set and retrieve these properties.

You would probably want to set the properties when creating the user or editing a user’s information. For this purpose you could use code similar to the following. Notice you need to call the Save after you set the properties.

Dim userProfile As ProfileCommon = ProfileCommon.Create(CreateUserWizard1.UserName, True)

With userProfile
   .FirstName = txtFirstName.Text.Trim
   .LastName = txtLastName.Text.Trim
   .Address1 = txtAddress1.Text.Trim
   .Address2 = txtAddress2.Text.Trim
   .City = txtCity.Text.Trim
   .State = drpState.SelectedItem.Value
   .Zip = txtZip.Text.Trim
   .PhoneNumber = txtPhoneNumber.Text.Trim
   .Save()
End With

You can then retrieve information for a user using code similar to the following.

Dim userProfile As ProfileCommon = ProfileCommon.Create(HttpContext.Current.User.Identity.Name, True)

            Dim strFirstName As String = userProfile.FirstName
            Dim strLastName As String = userProfile.LastName
            Dim strCompany As String = userProfile.Company
            Dim strAddress1 As String = userProfile.Address1
            Dim strAddress2 As String = userProfile.Address2
            Dim strCity As String = userProfile.City
            Dim strState As String = userProfile.State
            Dim strZip As String = userProfile.Zip
            Dim strPhoneNumber As String = userProfile.PhoneNumber

You could then display this information on an administration page.

That’s all there is to it. For a working example including a page to manage user information, you can register and download my User Management example project.



jeremy at 9:29 AM | (13) Comments | Add a comment | Permalink





Commments
Bartek said...

Hi Jeremy, I've been having a few problems with ASP.NET 2.0 and the profiles part of things and thought I'd run this question past you (love the blog by the way!) I have a custom property called "FullName". I have a form that allows logged in users to edit details about an item - how do I populate a dropdownlist with Profile.Fullname from every user? I've tried the following code, but I can't then seem to set the SelectedValue on the dropdownlist to equal the current value of the record... Protected Sub ddlAdminActionedBy_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Dim MyFormView As FormView = CType(LVImprovementView.FindControl("fvAdminImprovement"), FormView) Dim MyDDL As DropDownList = CType(MyFormView.FindControl("ddlAdminActionedBy"), DropDownList) Dim AllUsers As MembershipUserCollection = Membership.GetAllUsers() Dim User As MembershipUser For Each User In AllUsers MyDDL.Items.Add(Profile.GetProfile(User.ToString).FullName) Next Dim lstitem As ListItem = MyDDL.Items.FindByValue("N/A") If lstitem Is Nothing Then MyDDL.Items.Add("N/A") End If MyDDL.DataBind() End Sub Hope you can help! Bartek

Thursday, January 11, 2007 8:35 PM

jeremy said...

Bartek, I think the issue is that you are not setting the value. You might try something like the following. Dim MyDDL As New DropDownList Dim AllUsers As MembershipUserCollection = Membership.GetAllUsers() Dim newItem As New ListItem Dim User As MembershipUser For Each User In AllUsers newItem.Text = Profile.GetProfile(User.ToString).FirstName newItem.Value = Profile.GetProfile(User.ToString).Company MyDDL.Items.Add(newItem) Next

Thursday, January 11, 2007 8:38 PM

DigIdiot said...

Hi Jeremy, Ran across a post somewhere (or information here) this site was done with vis web dev but utilizes sql 2005 instead of sqlexpress. I'm having a very difficult time getting the same thing set up on my site. How did you get the dbs setup? My site has the ability to create dbs on their sql 2005 server. Do I create them and then somehow import the info from the sql express dbs in the Personal Website Starter kit? I assume my connection strings will need adjusting no matter what I do. I'm a little lost, if you can't tell. Like most things in web developement, I've just jumped into Visual web developer and though I'm finding away around that and have no worries about learning to use it, I need to get a site online so i can upload things and make sure they work. Anyway, any feedback you can provide would be helpful. I'm sure you get a lot of questions so no worries if you can't get to mine. thanks, Dig

Sunday, January 28, 2007 8:19 AM

jeremy said...

DigIdiot, Please register on my site if you haven't already. Then log in and post another comment under this topic. I'll then email you and we can discuss your original comment through email. Thanks.

Sunday, January 28, 2007 2:37 PM

boaz said...

Hi Jeremy. Great post! One question I had was - where should I place this code to read/write the user information? Also, where is this information saved? I'm trying to present the user with information from a table that uas a 'username' key field (its a monthly time sheet page on a company website). Thanks for your help!

Tuesday, September 16, 2008 11:35 AM

jeremy said...

I would recommend downloading my User Management sample project as it demonstrates reading and writing user profile information using a DataGrid.

Thursday, September 18, 2008 10:26 PM

dgdg said...

is this how i would allow a user to register and create thier own page? i want to allow users to register and build their own page. how do i do this? i just want a few simple criteria. name, photo, mp3, text.

Monday, May 04, 2009 1:33 PM

BravoOsca said...

Hi Jeremy, Hope you can help me, I have downloaded your fine project, but when im making my own project with your user management system, i keep getting "ProfileCommon is not defined". I realy have tryed everything, but i can't figure it out. Please contact me, if you need some code or more info.

Wednesday, November 18, 2009 12:30 PM

jeremy said...

You need to specify the properties in <profile> section in the web.config as shown above.

Sunday, November 29, 2009 9:36 PM

online blackjack said...

ASP.Net or Silverlight application. For whatever reason, being able to immediately connect to your corporate domain may be difficult.

Tuesday, August 03, 2010 5:37 AM

agile informatics fremont said...

I noticed that you arrived here from Google looking for membership users in ASP.NET 2.0. The below article is probably best suited for you based on the provided keywords. However to aid you on your search here's a list of related blog posts.

Friday, August 13, 2010 6:09 AM

town car said...

the execution appears to have been rather uneven. It would seem that some stores implemented this temporary department more fully than other stores.

Friday, August 13, 2010 7:58 AM

maison de credit said...

This support looks absolutely perfect. All these tinny details are made with lot of background knowledge about website design. Keep on it taking action!

Wednesday, August 25, 2010 4:26 AM

  Leave a comment

Enter Your Name:
 
Enter your website
Security code from image below:  
Need Custom Controls Work or Training?
   


Join WebHost4Life.com






If you would like to help support this project, please click the button below to make a small donation.