Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DOB           textbox1(Calendar image)
Wedding Day   textbox2(calendar image)


i am validating DOB and Wedding Day should not be the same.for that code as follows;
C#
DateTime DOB = Convert.ToDateTime(FromDate.ToString());
                DateTime Wed = Convert.ToDateTime(Todate.ToString());

          if (DOB.Day = Wed.Day && DOB.Month == Wed.Month && DOB.Year == Wed.Year)
                {
               Label6.Text = "Date of Birth And Wedding day should not be the same";
                }

but when i run error message shows;

Operator '&&' cannot be applied to operands of type 'int' and 'bool'

from my above code.please give the correct code.
Posted
Updated 25-Dec-12 21:00pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Dec-12 3:17am    
Is that for people who want to have more celebrations? :-)
—SA

C#
if (DOB.Day = Wed.Day ...

Should be
C#
if (DOB.Day == Wed.Day ...
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Dec-12 3:20am    
Yes, I caught this one, too, a 5. But the right recipe to solve the problem is solution 2.
—SA
__TR__ 26-Dec-12 4:30am    
Thanks SA. You are right, solution 2 is the correct approach.
You can use the below code..
C#
if( DOB.Date == Wed.Date)
{
   //invalid 
}
else
{
    //valid 
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Dec-12 3:21am    
Best advice so far, a 5.
—SA
__TR__ 26-Dec-12 4:27am    
+5
Quote:
if (DOB.Day = Wed.Day && DOB.Month == Wed.Month && DOB.Year == Wed.Year)

You used the assignment operator (=) instead of the equality one (==).

By the way, why don't you compare directly DateTime variables?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Dec-12 3:20am    
Yes, I caught this one, too, a 5. But the right recipe to solve the problem is solution 2.
—SA
__TR__ 26-Dec-12 4:28am    
+5
use DateTime.Compare(DateTime1, DateTime2) method for compairing date

if (DateTime.Compare(DOB, Wed) == 0)
{
     Label6.Text = "Date of Birth And Wedding day should not be the same";
}
 
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