Hi Aamir,
The error is clear from the error msg - the value in the text box is not a datetime. You can use
TryParse()
as Peter suggested. Or you can catch a
FormatException
to let the user know about the problem at runtime.
try
{
DateTime dt = DateTime.Parse(txt_DOB.Text);
int Validate = DateTime.Now.Year - dt.Year;
}
catch (FormatException)
{
MessageBox.Show("This is not a valid date time.");
}