Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one field in design page as follows;

Date of Birth textbox1(calendar image)

i want to validate dateof birth date should be greater than the todays date and more than the todays date.

for that i write the code as follows;

DateTime DOB = Convert.ToDateTime(FromDate.ToString());

if(FromDate.SelectedDateValue > DateTime.Now)
{
Label6.Text = ("you have not born yet");
}
i try the above code when i run i show the error message as follows;

Operator '>' cannot be applied to operands of type 'string' and 'System.DateTime.


for the above code please correct my code and reply.


thanks,
Posted

Hi , try this
C#
DateTime compare;
if(!DateTime.TryParse(FromDate.SelectedDateValue, out compare))
//fail to parse data
var result =DateTime.Compare(compare,DateTime.Now);
if(resul>0)
Label6.Text = ("you have not born yet");
 
Share this answer
 
v2
Comments
[no name] 26-Dec-12 5:42am    
Beautiful coding...
I appreciate using "DateTime.TryParse()".
Oleksandr Kulchytskyi 26-Dec-12 5:46am    
Thanks :)
I alway try to use TryParse methods, because it's a robust approach not so error prone like would be in case of usage Parse methods =)
Try some thing like below code.

C#
if (DOB.CompareTo(DateTime.Now) > some_integer_value)
           {
               //your required message
           }
 
Share this answer
 
You are comparing with a string! you can use DOB object itself

Use as below..

C#
if(DOB.Date > DateTime.Now.Date) //Now holds time as well.
{
Label6.Text = ("you have not born yet");
}
 
Share this answer
 
VB
Try This....

It working....



if (DateTime.Compare(FromDate.SelectedDateValue, DateTime.Now) > 0)
            {

                Label6.Text ="you have not born yet";


            }
 
Share this answer
 
C#
DateTime val;
if(!DateTime.TryParse(SelectedDate, out val))
Console.WriteLine("error");
else
if(val.Ticks - DateTime.Now.Ticks > 0)
Label6.Text = ("you have not born yet");
 
Share this answer
 

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