mathieu_cupryk
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/
<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; }
}
ASKER
this is what I have now?
How can I modify the below?
try
{
MembershipCreateStatus status;
MembershipUserCollection list = Membership.FindUsersByEmai l(this.txt MailFrom.T ext);
if (list.Count == 0)
{
MembershipUser user = Membership.CreateUser(this .txtUserNa me.Text, this.txtPassword.Text, this.txtMailFrom.Text, null, null, false, null, out status);
if (MembershipCreateStatus.Su ccess == status)
{
ProfileCommon pc = Profile.GetProfile(user.Us erName);
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.Da teDDL1.Dat e, System.Globalization.Cultu reInfo.Inv ariantCult ure);
pc.Personal.Age = getAge(this.DateDDL1.Date) ;
pc.Personal.Gender = this.ddlGender.SelectedVal ue;
pc.Personal.Seeking = this.ddlSeeking.SelectedVa lue;
pc.Personal.ConfirmationGU ID = System.Guid.NewGuid().ToSt ring("N");
pc.Personal.UserIP = Request.UserHostAddress;
pc.Personal.SessionID = this.Session.SessionID;
pc.Address.Country = this.ddlCountryRegister.Se lectedValu e;
pc.Address.Region = this.ddlRegion.SelectedVal ue;
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.NewMemb erEmail;
sData = sData.Replace("[Name]", this.txtFirstName.Text.Tri m());
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("web master@ome galove.com ", "OmegaLove", this.txtMailFrom.Text.Trim (),
sData, "New Member Activation", false);
Session["UserID"] = iUserID;
Response.Redirect("Registe red.aspx") ;
}
else
{
// first else block:
switch (status)
{
case MembershipCreateStatus.Dup licateUser Name:
lblMessage.Text = "There already exists a user with this username.";
break;
case MembershipCreateStatus.Dup licateEmai l:
lblMessage.Text = "There already exists a user with this email address.";
break;
//case MembershipCreateStatus.Inv alidEmail:
// lblMessage.Text = "There email address you provided in invalid.";
// break;
//case MembershipCreateStatus.Inv alidAnswer :
// lblMessage.Text = "There security answer was invalid.";
// break;
//case MembershipCreateStatus.Inv alidPasswo rd:
// 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 can I modify the below?
try
{
MembershipCreateStatus status;
MembershipUserCollection list = Membership.FindUsersByEmai
if (list.Count == 0)
{
MembershipUser user = Membership.CreateUser(this
if (MembershipCreateStatus.Su
{
ProfileCommon pc = Profile.GetProfile(user.Us
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.Da
pc.Personal.Age = getAge(this.DateDDL1.Date)
pc.Personal.Gender = this.ddlGender.SelectedVal
pc.Personal.Seeking = this.ddlSeeking.SelectedVa
pc.Personal.ConfirmationGU
pc.Personal.UserIP = Request.UserHostAddress;
pc.Personal.SessionID = this.Session.SessionID;
pc.Address.Country = this.ddlCountryRegister.Se
pc.Address.Region = this.ddlRegion.SelectedVal
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.NewMemb
sData = sData.Replace("[Name]", this.txtFirstName.Text.Tri
sData = sData.Replace("[LINK]", "http://www.omegalove.com/Activate.aspx?ID=" + userId.ToString());
sData = sData.Replace("[UserName]"
sData = sData.Replace("[Pwd]", this.txtPassword.Text.Trim
SMTPManager.SendEmail("web
sData, "New Member Activation", false);
Session["UserID"] = iUserID;
Response.Redirect("Registe
}
else
{
// first else block:
switch (status)
{
case MembershipCreateStatus.Dup
lblMessage.Text = "There already exists a user with this username.";
break;
case MembershipCreateStatus.Dup
lblMessage.Text = "There already exists a user with this email address.";
break;
//case MembershipCreateStatus.Inv
// lblMessage.Text = "There email address you provided in invalid.";
// break;
//case MembershipCreateStatus.Inv
// lblMessage.Text = "There security answer was invalid.";
// break;
//case MembershipCreateStatus.Inv
// 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?
ASKER
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.IsAuthentic ated)
{
myUserProfile = HttpContext.Current.Profil e as ProfileCommon;
}
tbFirstname.Text = myUserProfile.FirstName;
and so forth... is that what you mean?
You'll need these
public MembershipUser myUser;
public ProfileCommon myUserProfile;
if (User.Identity.IsAuthentic
{
myUserProfile = HttpContext.Current.Profil
}
tbFirstname.Text = myUserProfile.FirstName;
and so forth... is that what you mean?
ASKER
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.Profil e as ProfileCommon;
lbFirstNameLabel.Text = myUserProfile.FirstName;
They will need to be logged in.
ProfileCommon myUserProfile = HttpContext.Current.Profil
lbFirstNameLabel.Text = myUserProfile.FirstName;
They will need to be logged in.
ASKER
i have three classes of information?
what are we doing hereme.
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.
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.
ASKER
so I created these classes for nothing?
what should I do?
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. :)
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.
And not worry about classes ... .NET does it for me. :)
ASKER
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. :)
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. :)
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
subsonic is great.
Is this better.
<profile defaultProvider="OL_Profil eProvider" >
<providers>
<add name="OL_ProfileProvider" connectionStringName="Loca lSqlServer " type="System.Web.Profile.S qlProfileP rovider"/>
</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.
Is this better.
<profile defaultProvider="OL_Profil
<providers>
<add name="OL_ProfileProvider" connectionStringName="Loca
</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.
Is this what you're after?
Open in new window