German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Managing Age of a User from Profile field Birthday

Written by omerkamal on Jan 05, 2007
How to Populate Birthday Dropdownlist, Save Data to Birthday Profile Field and Display Age of the user

Explanation:

We will start with how to create and populate the Birthday DropDownlist in the Registration Page. Actually, we are using three dropdownlists to get our desired result. 1st Dropdownlist is for Days, 2nd Months and 3rd for Years.

Just drag three DropdownlistBoxes to your .aspx Page.
















Now, Create three Functions in codebehind for each of the Dropdownlist to populate them.

public void GetDaysListed(DropDownList DDDayList)
{
for (int i = 1; i < 32; i++)
{
ListItem list = new ListItem();
list.Text = i.ToString();
list.Value = i.ToString();
DDDayList.Items.Add(list);
}
ListItem mlist = new ListItem();
mlist.Text = "[Day]";
mlist.Value = "[Day]";
mlist.Selected = true;
DDDayList.Items.Add(mlist);

}

public void GetMonthsListed(DropDownList DDMonthList)
{
DateTime month = Convert.ToDateTime("1/1/2007");
for (int i = 0; i < 12; i++)
{

DateTime NextMont = month.AddMonths(i);
ListItem list = new ListItem();
list.Text = NextMont.ToString("MMMM");
list.Value = NextMont.Month.ToString();
DDMonthList.Items.Add(list);
}

ListItem mlist = new ListItem();
mlist.Text = "[Month]";
mlist.Value = "[Month]";
mlist.Selected = true;
DDMonthList.Items.Add(mlist);
}

public void GetYearsListed(DropDownList DDYearList)
{
int yearLast = DateTime.Now.Year-10;
int yearThen = yearLast - 90;
for (int i = yearThen; i < yearLast; i++)
{
ListItem list = new ListItem();
list.Text = yearThen.ToString();
list.Value = yearThen.ToString();
DDYearList.Items.Add(list);
yearThen += 1;
}

ListItem mlist = new ListItem();
mlist.Text = "[Year]";
mlist.Value = "[Year]";
mlist.Selected = true;
DDYearList.Items.Add(mlist);

You noticed that we are inserting an extra Item to our every DropDownlistBox. This is because of three reasons.

  1. To give our users liberty of Optional behaviour.
  2. To Inform Users of fields data types
  3. Restrict users to enter what we want.

We will Pass the DropDownList objects to each of the function. It will be performed at the Page_Load event. One more thing, these three DropDownlistBoxes are embeded in to the "CreateUserWizardStep" of "CreateUserWizard". We will use " FindControl" function of "ContentTemplateContainer" which lies in "CreateUserStep". Where as "CreateUserStep" is the First Step of "CreateUserWizard".

Our Page_Load look like this;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetYearsListed(
(DropDownList)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListYear"));
GetMonthsListed(
(DropDownList)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListMonth"));
GetDaysListed( (DropDownList)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListDay"));
}
}

We are Casting the "FindControl" to our DropDownlist Control type.

Now, we are going to save the users choice in the Profile. Before going next we should know how to save and retrive profiles in ASP .NET 2.0. See the Following articles for the basics of Profilling.

  1. To create UserPrifile page? See our Article Profiling in ASP .NET 2.0.
  2. To create Profile in the Registration Page see Create a User Profile in Regisration Page.

We added One more field to our Profile in the Web.config. Data Type of the Field is DateTime.

<add name="Bday" type="System.DateTime"/>

Now, see the code to save Birthday:

DateTime bday = new DateTime(
Int32.Parse( ((DropDownList)
WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
(
"DropDownListYear")).SelectedValue),
Int32.Parse( ((DropDownList)
WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
(
"DropDownListMonth")).SelectedValue),
Int32.Parse( ((DropDownList)
WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
(
"DropDownListDay")).SelectedValue) 
);
p.Bday = bday;
p.Save();

Thats it, now profile have the Birthday of the newly registerd user. Next we will see the trick to show Birthday in the userProfile.aspx Page.

MembershipUser ThisUser = Membership.GetUser(UserName);

if (ThisUser != null)
{
   ProfileCommon Prof = Profile.GetProfile(UserName);
if (Prof.Bday != null
&& Prof.Bday.Year> 1907)
{

TimeSpan timespn = DateTime.Now.Subtract(Prof.Bday);
int years = (int)(timespn.TotalDays / 365.25);
LabeldataBd.Text = "Age:" +
Convert.ToString(years);

}

We are checking the Profile.Bday.Year that if it is Greater then 1907 or not (which is our least value in the Year DropDownListBox). Why? because if your page is already running and you enter a new Profile Field "Bday" to the existing Profile it will generate a dummy value for all users. We want some how to check the dummy value creation. In my case it enterd "01/01/0001" to each user.
Where as, UserName can be Taken from QueryString. 

string UserName=Request.QueryString("UserName");

Visitors/Readers Comments
(for questions please use The Forum)



Ranganath
your Articles are very valuable

27/07/2007 06:24:24 UTC

amagondes
Hi, Thanks for that.. it was very useful

27/12/2007 17:21:15 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)