using complex types for profile information provider.

Posted on 2009-02-16
Medium Priority
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
  • 8
  • 8

Expert Comment

ID: 23655966
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

ID: 23656016
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]", "http://www.omegalove.com/Activate.aspx?ID=" + userId.ToString());
                        sData = sData.Replace("[UserName]", this.txtUserName.Text.Trim());
                        sData = sData.Replace("[Pwd]", this.txtPassword.Text.Trim());
                        SMTPManager.SendEmail("webmaster@omegalove.com", "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;


Expert Comment

ID: 23656052
How do you mean? What are you trying to do that's not working?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 23656059
how can I see the information for the Personal?

Expert Comment

ID: 23656082
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

ID: 23656138
no for now lets start of with the basics just to show user information.

Expert Comment

ID: 23656168
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

ID: 23656172
i have three classes of information?
what are we doing hereme.

Expert Comment

ID: 23656183
You don't need your class file, that's the whole point of the <properties> with asp.net 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

ID: 23656284
so I created these classes for nothing?
what should I do?

Expert Comment

ID: 23656302
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

ID: 23656330
why do people create their own classes.

Expert Comment

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

In asp.net, 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

ID: 23662713
do u know of any good source code other than asp.net site that has a good examples of what u are mentioning.

Accepted Solution

NauticalNonsense earned 2000 total points
ID: 23662971
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

ID: 23665141
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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