using complex types for profile information provider.

Posted on 2009-02-16
Last Modified: 2012-05-06
I have this in my web.config
        <add name="Personal" type="Personal" />
        <add name="Address" type="Address" />
        <add name="Preferences" type="Preferences" />

I created a class for each one.
Now I must create a register for this?
I am not sure how to tackle this. I looked on the net there are only a few resources
which do not have a good explaination.
Any help would be kind/

example of first class.

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

/// <summary>

/// Summary description for Personal

/// </summary>


public class Personal


    public Personal() { }

    public Personal


         string username,

         string password,

         string email,

         string firstname,

         string lastname,

         DateTime dob,

         int age,

         string gender,

         string seeking,

         string confirmationguid,

         string userid,

         string sessionid)


        this.UserName = username;

        this.Password = password;

        this.Email = email;

        this.FirstName = firstname;

        this.LastName = lastname;

        this.DOB = dob;

        this.Age = age;

        this.Gender = gender;

        this.Seeking = seeking;

        this.ConfirmationGUID = confirmationguid;

        this.UserIP = userip;

        this.SessionID = sessionid;


    private string username = string.Empty;

    public string UserName { get; set; }

    private string password = string.Empty;

    public string Password { get; set; }

    private string email = string.Empty;

    public string Email { get; set; }

    private string firstname = string.Empty;

    public string FirstName { get; set; }

    private string lastname = string.Empty;

    public string LastName { get; set; }

    private DateTime dob = DateTime.MinValue;

    public DateTime DOB { get; set; }


    private int age = 0;

    public int Age { get; set; }


    private string gender = string.Empty;

    public string Gender { get; set; }


    private string seeking = string.Empty;

    public string Seeking { get; set; }

    private string confirmationguid = string.Empty;

    public string ConfirmationGUID { get; set; }


    private string userip = string.Empty;

    public string UserIP { get; set; }


    private string sessionid = string.Empty;

    public string SessionID { get; set; }



Open in new window

Question by:mathieu_cupryk
    LVL 7

    Expert Comment

    Create a page with textboxes for the information you want to capture.
    Is this what you're after?
                // create the user, log them in
                MembershipUser memberNewUser = Membership.CreateUser(tbScreenName.Text.Trim(), tbPassword1.Text, tbEmail.Text);
                memberNewUser.IsApproved = false;
                FormsAuthentication.SetAuthCookie(memberNewUser.UserName, true);
                // and fill in the default profile stuff
                ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(memberNewUser.UserName, true);
                profile.Firstname = tbFirstName.Text;
                profile.Lastname = tbLastname.Text;
    	    // continue adding your properties...

    Open in new window


    Author Comment

    this is what I have now?
    How can I modify the below?
                    MembershipCreateStatus status;
                    MembershipUserCollection list = Membership.FindUsersByEmail(this.txtMailFrom.Text);

                    if (list.Count == 0)

                        MembershipUser user = Membership.CreateUser(this.txtUserName.Text, this.txtPassword.Text, this.txtMailFrom.Text, null, null, false, null, out status);

                        if (MembershipCreateStatus.Success == status)
                            ProfileCommon pc = Profile.GetProfile(user.UserName);
                            Guid userId = (Guid)user.ProviderUserKey;
                            pc.Personal.UserName = this.txtUserName.Text;
                            pc.Personal.Password = this.txtPassword.Text;
                            pc.Personal.Email = this.txtMailFrom.Text;
                            pc.Personal.FirstName = this.txtFirstName.Text;
                            pc.Personal.LastName = this.txtLastName.Text;
                            pc.Personal.DOB = Convert.ToDateTime(this.DateDDL1.Date, System.Globalization.CultureInfo.InvariantCulture);
                            pc.Personal.Age = getAge(this.DateDDL1.Date);
                            pc.Personal.Gender = this.ddlGender.SelectedValue;
                            pc.Personal.Seeking = this.ddlSeeking.SelectedValue;
                            pc.Personal.ConfirmationGUID = System.Guid.NewGuid().ToString("N");
                            pc.Personal.UserIP = Request.UserHostAddress;
                            pc.Personal.SessionID = this.Session.SessionID;

                            pc.Address.Country = this.ddlCountryRegister.SelectedValue;
                            pc.Address.Region = this.ddlRegion.SelectedValue;
                            pc.Address.City = this.ddlCity.SelectedValue;
                            pc.Address.ZipCode = this.txtZipCode.Text;

                            lblMessage.Text = "User created successfully!";
                            iUserID = this.txtUserName.Text;

                            string sData = Resources.Resource.NewMemberEmail;
                            sData = sData.Replace("[Name]", this.txtFirstName.Text.Trim());
                            sData = sData.Replace("[LINK]", "" + userId.ToString());
                            sData = sData.Replace("[UserName]", this.txtUserName.Text.Trim());
                            sData = sData.Replace("[Pwd]", this.txtPassword.Text.Trim());
                            SMTPManager.SendEmail("", "OmegaLove", this.txtMailFrom.Text.Trim(),
                                 sData, "New Member Activation", false);
                            Session["UserID"] = iUserID;
                            // first else block:
                            switch (status)
                                case MembershipCreateStatus.DuplicateUserName:
                                    lblMessage.Text = "There already exists a user with this username.";
                                case MembershipCreateStatus.DuplicateEmail:
                                    lblMessage.Text = "There already exists a user with this email address.";
                                //case MembershipCreateStatus.InvalidEmail:
                                //    lblMessage.Text = "There email address you provided in invalid.";
                                //    break;
                                //case MembershipCreateStatus.InvalidAnswer:
                                //    lblMessage.Text = "There security answer was invalid.";
                                //    break;
                                //case MembershipCreateStatus.InvalidPassword:
                                //    lblMessage.Text = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
                                //    break;
                                    lblMessage.Text = "There was an unknown error; the user account was NOT created.";


                        lblMessage.Text = "User already exists.";
                catch (Exception ex)
                    lblMessage.Text = ex.Message;

    LVL 7

    Expert Comment

    How do you mean? What are you trying to do that's not working?

    Author Comment

    how can I see the information for the Personal?
    LVL 7

    Expert Comment

    Like in an Edit My Profile page?

    You'll need these
    public MembershipUser myUser;
    public ProfileCommon myUserProfile;

    if (User.Identity.IsAuthenticated)
                    myUserProfile = HttpContext.Current.Profile as ProfileCommon;

    tbFirstname.Text = myUserProfile.FirstName;

    and so forth... is that what you mean?

    Author Comment

    no for now lets start of with the basics just to show user information.
    LVL 7

    Expert Comment

    That is the basic... on the page you want to display user information

    ProfileCommon myUserProfile = HttpContext.Current.Profile as ProfileCommon;
    lbFirstNameLabel.Text = myUserProfile.FirstName;

    They will need to be logged in.

    Author Comment

    i have three classes of information?
    what are we doing hereme.
    LVL 7

    Expert Comment

    You don't need your class file, that's the whole point of the <properties> with membership. You add properties, and .NET creates the wrapper class for you to use.

    You can find a ton of information on implementing a "custom provider" for .NET, if their stuff doesn't work for you... but I have yet to need to do that.

    Author Comment

    so I created these classes for nothing?
    what should I do?
    LVL 7

    Expert Comment

    If you are just using properties like firstname, lastname, and string and numeric and boolean values and such, then no, you do not need to create your own classes: that's what the membership provider does.

    The way I have mine set up is a base page class that sees if the user is logged in. That way every page in my application knows about the user, and any time i need I can just say

    lbHello.Text="Hello, " + myPageclass.myUserProfile.FirstName;

    And not worry about classes ... .NET does it for me. :)

    Author Comment

    why do people create their own classes.
    LVL 7

    Expert Comment

    Creating a class is a way to encapsulae functionality so that other members can use it.

    In, they created all of the profile classes so that you wouldn't have to. But lets say you have another table in your database, "News"... You could create a class for your News table, which would expose the properties (NewsTitle, Author, NewsDate) etc, as well as create Methods for saving, updating, or whatever. So in your page you could create your news class

    NewsItem n = new NewsItem();
    n.Load(5); // the news ID
    lbNewsTitle.Text = n.NewsTitle;

    And then if you had a sidebar, you could do the same thing, and have that same class used.

    It's all part of OOP. :)

    Author Comment

    do u know of any good source code other than site that has a good examples of what u are mentioning.
    LVL 7

    Accepted Solution

    Always been a fan of SitePoint:

    And for a data access layer, I can't recommend Subsonic enough. It's an amazing thing...

    With subsonic, view the videos... basically, it wraps all these class files around your database schema, so you can literally do:

    News n = new News();

    And that saves your record to your database.
    It's amazing.

    Author Comment

    subsonic is great.

    Is this better.

     <profile defaultProvider="OL_ProfileProvider">
              <add name="OL_ProfileProvider" connectionStringName="LocalSqlServer" type="System.Web.Profile.SqlProfileProvider"/>
            <group name="Personal">
              <add name="UserName" type="System.String"/>
              <add name="Password" type="System.String"/>
              <add name="Email" type="System.String"/>
              <add name="FirstName" type="System.String"/>
              <add name="LastName" type="System.String" />
              <add name="DOB" type="System.DateTime" />
              <add name="Age" type="System.Int32" />
              <add name="Gender" type="System.String"/>
              <add name="Seeking" type="System.String"/>
              <add name="Country" type="System.String"/>
              <add name="Region" type="System.String"/>
              <add name="City" type="System.String"/>
              <add name="ZipCode" type="System.String"/>
              <add name="ConfirmationGUID"  type="System.String"/>
              <add name="UserIP"  type="System.String"/>
              <add name="SessionID"  type="System.String"/>
            <group name="Address">
              <add name="Country" type="System.String"/>
              <add name="Region" type="System.String"/>
              <add name="City" type="System.String"/>
              <add name="ZipCode" type="System.String"/>
            <group name="Preferences">
              <add name="Theme" type="String" allowAnonymous="false"/>
              <add name="Culture" type="String" defaultValue="en-US"/>

    Is it good to use this instead of those classes.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now