Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 25-Dec-12 22:59pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi , try this
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");
  Permalink  
v2
Comments
Oleksandr Kulchytskyi at 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 =)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try some thing like below code.
 
 if (DOB.CompareTo(DateTime.Now) > some_integer_value)
            {
                //your required message
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You are comparing with a string! you can use DOB object itself
 
Use as below..
 
if(DOB.Date > DateTime.Now.Date) //Now holds time as well.
{
Label6.Text = ("you have not born yet");
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Try This....
 
It working....
 

 
if (DateTime.Compare(FromDate.SelectedDateValue, DateTime.Now) > 0)
            {
 
                Label6.Text ="you have not born yet";
 

            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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");
  Permalink  

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

  Print Answers RSS
0 CPallini 365
1 Sergey Alexandrovich Kryukov 242
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,135
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100