Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

using complex types for profile information provider.

I have this in my web.config
<properties>
        <add name="Personal" type="Personal" />
        <add name="Address" type="Address" />
        <add name="Preferences" type="Preferences" />
      </properties>

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>
[Serializable()]
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

Avatar of NauticalNonsense
NauticalNonsense
Flag of United States of America image

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...
            profile.Save();

Open in new window

Avatar of mathieu_cupryk

ASKER

this is what I have now?
How can I modify the below?
 try
            {
                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;
                       
                        pc.Save();

                        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;
                        Response.Redirect("Registered.aspx");
                    }
                    else
                    {
                        // first else block:
                        switch (status)
                        {
                            case MembershipCreateStatus.DuplicateUserName:
                                lblMessage.Text = "There already exists a user with this username.";
                                break;
                            case MembershipCreateStatus.DuplicateEmail:
                                lblMessage.Text = "There already exists a user with this email address.";
                                break;
                            //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;
                            default:
                                lblMessage.Text = "There was an unknown error; the user account was NOT created.";
                                break;
                        }
                       

                    }

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

            }
How do you mean? What are you trying to do that's not working?
how can I see the information for the Personal?
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?
no for now lets start of with the basics just to show user information.
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.
i have three classes of information?
what are we doing hereme.
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.
so I created these classes for nothing?
what should I do?
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. :)
why do people create their own classes.
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. :)
do u know of any good source code other than asp.net site that has a good examples of what u are mentioning.
ASKER CERTIFIED SOLUTION
Avatar of NauticalNonsense
NauticalNonsense
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
subsonic is great.

Is this better.

 <profile defaultProvider="OL_ProfileProvider">
        <providers>
          <add name="OL_ProfileProvider" connectionStringName="LocalSqlServer" type="System.Web.Profile.SqlProfileProvider"/>
        </providers>
      <properties>
        <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>
        <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>
        <group name="Preferences">
          <add name="Theme" type="String" allowAnonymous="false"/>
          <add name="Culture" type="String" defaultValue="en-US"/>
        </group>
      </properties>
    </profile>

Is it good to use this instead of those classes.