I have a textbox – DateOfBirth
I follow following steps from validating data to saving it in database.
Step 1: When I submit text, I validate if date is in correct format(dd/mm/yyyy):
string Pattern = @"^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/](19|20)\d\d";
Step 2: If format is correct, I convert input value of textbox to datetime format:
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
DOB = DateTime.ParseExact(txtDOB.Value, "dd/mm/yyyy", theCultureInfo);
Step 3: Then I send value to database and save as it is in my table.
I got an issue here:
For date “04/04/2012” everything works fine.
But if I submit date “4/04/2012” or “04/4/2012”, the step 2 throws an exception:
String was not recognized as a valid DateTime.
How do I handle this issue ?
Should I escape step 2 and do conversion in database query instead ???
because the method expects that you have two places for the date and you only supplied one value, so to solve your problem, remove one
dinParseExactformat.