Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have done the below code to calculate my age by filling the date of birth,I get the conversion failure problem

What I have tried:

C#
<pre> protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
       {
            txtDob.Text = Calendar1.SelectedDate.ToString();
            Calendar1.Visible = false;
            int year = DateTime.Now.Year - Calendar1.SelectedDate.Year;
            int months = DateTime.Now.Month - Calendar1.SelectedDate.Month;

            string ageval = ((year - 1) + " years" + (-12 - months) + " months");

            if (months > 12)
            {
                year = year + 1;
            }
            int value = int.Parse(ageval);
            value = (ToString(txtAge.Text)) ;

SQL
Name varchar(50),Department varchar (50),Salary int
 DOJ datetime,DOB datetime,Age int,Country varchar(50),Stat varchar(50),Phone bigint,
 Email varchar(100),Pincode bigint)
Posted
Updated 22-Jan-17 23:00pm
Comments
CPallini 18-Jan-17 3:10am    
Please explain better. What is the exact error message (if any)? What is the offending line of code?

Um ... that's some odd code.
C#
string ageval = ((year - 1) + " years" + (-12 - months) + " months");
...
int value = int.Parse(ageval);
value = (ToString(txtAge.Text)) ;

So you load a string with a non-integer value deliberately, then try to convert that to an integer, which you then try to overwrite with a string-ified version of some random user input. And you are surprised when it fails? It won't even compile, unless your ToString method is very, very badly named!

Have a look at this: Working with Age: it's not the same as a TimeSpan![^] - it does all the "legwork" for you.
 
Share this answer
 
Comments
Member 12605293 18-Jan-17 3:40am    
Hi Can you help me how to modify this code to get the result.I am new to programming.
OriginalGriff 18-Jan-17 3:49am    
Follow the link I gave you: it includes a demo of how to work out and display ages.
Maciej Los 18-Jan-17 15:58pm    
5ed!
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            int year =Convert.ToInt32(DateTime.Now.Year);
            int month = Convert.ToInt32(DateTime.Now.Month);
            int day = Convert.ToInt32(DateTime.Now.Day);

            int dobYear =  Calendar1.SelectedDate.Year;
            int dobMonth = Calendar1.SelectedDate.Month;
            int dobDate = Calendar1.SelectedDate.Day;

            int ageDay = 0;
            int ageMonth = 0;
            int ageYear = 0;

            if (day >=  dobDate)
            {
                ageDay = day - dobDate;
            }
            else if(day< dobDate)
            {
                // If current Month= Jan, March, May, July, August, Oct, Dec i.e. of 31 days
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                {
                    day = day + 31;
                }
                //  if current Month is of 30 days.
                else if (month == 4 || month == 6 || month == 9 || month == 11) 
                {
                    day = day + 30;
                }
                // If Month is February
                else if (month == 2)
                {
                    if(year % 4==0) // if Year is Leap year then Feb will be of 29 days.
                    {
                        day = day + 29;
                    }
                    else
                    {
                        day = day + 28;
                    }
                }
                ageDay = day - dobDate;
                month = month - 1;
            }

            if (month >= dobMonth)
            {
                ageMonth = dobMonth - month;
            }
            else if (month < dobMonth)
            {
                month = month + 12;
                year = year - 1;
                ageMonth = month - dobMonth;               
            }

            ageYear = year - dobYear;
            string sAge = String.Format("{0} Year/s {1} Month/s {2} Day/s", ageYear, ageMonth, ageDay);
            TextBox1.Text = sAge;
       

        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900