hi,

i was working on calculate the birthdate when user select the date,month,year by combobox. the age should display on textbox. i have wrote some code but it show the
error messsage.

can any one solve this please

``````public static int CalculateAge(DateTime birthDate)
{
DateTime now = DateTime.Today;
int years = now.Year - birthDate.Year;
if
(now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))

--years;

return years;
}``````

on button click event the following coding is working

``````int year = Convert.ToInt32(comboBox1.Text );

int month = Convert.ToInt32(comboBox2.Text); // it contain month in string format

int day = Convert.ToInt32(comboBox3.Text );

[B]           DateTime birthDate = new DateTime(year, month, day);[/B]   // error

error message = Year, Month, and Day parameters describe an un-representable DateTime.

What are the values that you are putting in the comboboxes? Rather than use Convert try using

``````Int32.Parse()
``````

or

``````Int32.TryParse()
``````

and check the values before you put them into the DateTime constructor. I'll have to check if the DateTime constructor makes sure that the date is in a reasonable range.

Also, why not use a date/time picker instead of your own comboboxes? It gives you data in the right form already and you can use the TimeSpan object to get the difference between the dates.

This is a variation of your CalculateAge method.
I used the operator overloading features of DateTime, so I would not have to work with a huge boolean expression.
Feel free to use it or not.

``````public static int CalculateAge(DateTime birthDate)
{
DateTime testDate = new DateTime(DateTime.Today.Year, birthDate.Month, birthDate.Day);
int years = DateTime.Today.Year - birthDate.Year;
if (testDate > DateTime.Today) //you still have not had your birthday this year
years--;
return years;
}``````

It gives you a Value property wich is of type DateTime.

before using the combobox I was tried to code on DateTimePicker but i don't know how to perform coding on it . because in combobox i have seperate Text to calculate the year only I know it's wrong method to code but I realy don't have any idea about DateTimePicker property.

if u don't mind can u explain me which prperty will work to solve my problem..

``````DateTime dt = dateTimePicker1.Value;
TimeSpan ts = DateTime.Now - dt;``````

Then use `ts.TotalDays` to do your calculation.