Problem with Invalid Date
-
I'm facing some issues with the date thing: While debugging, I can see that the value of stringDate is coming as "3/60/1989" (MM/DD/YYYY), which is practically not possible, but these value is coming from the database as it is wrongly entered there. Now when this stringDate is parsed into DateTime format, it gives me a different date (i.e. the value of dtDate is shown as "6/30/1989") and throws an exception "Invalid DateTime Entry" at this line:
dtDate = DateTime.Parse(stringDate);
if (dtDate > startDate && dtDate < endDate)
{
row["NEW_DATE_FIELD"] = stringDate;
myTable.ImportRow(row);
}So, now I want that my code to do the following:
To enter a "INVALID DATE" message to the row["NEW_DATE_FIELD"] instead of the wrong date as such
Note : In VB.NET we have a built-in method named**IsDate()**
, which receives a string as parameter and return True if the string is a date or false. Is there any short-cut method in C# equivalent to IsDate method ? Any help would be appreciated. -
I'm facing some issues with the date thing: While debugging, I can see that the value of stringDate is coming as "3/60/1989" (MM/DD/YYYY), which is practically not possible, but these value is coming from the database as it is wrongly entered there. Now when this stringDate is parsed into DateTime format, it gives me a different date (i.e. the value of dtDate is shown as "6/30/1989") and throws an exception "Invalid DateTime Entry" at this line:
dtDate = DateTime.Parse(stringDate);
if (dtDate > startDate && dtDate < endDate)
{
row["NEW_DATE_FIELD"] = stringDate;
myTable.ImportRow(row);
}So, now I want that my code to do the following:
To enter a "INVALID DATE" message to the row["NEW_DATE_FIELD"] instead of the wrong date as such
Note : In VB.NET we have a built-in method named**IsDate()**
, which receives a string as parameter and return True if the string is a date or false. Is there any short-cut method in C# equivalent to IsDate method ? Any help would be appreciated. -
DateTime.TryParse[^] You should be able to work it out from there ;)
it's showing the compile error :
'System.DateTime' does not contain a definition for 'TryParse'
do i need to add any other namespaces or references. -
it's showing the compile error :
'System.DateTime' does not contain a definition for 'TryParse'
do i need to add any other namespaces or references. -
TryParse is only available in .NET framework 2.0 or later. If you are using version 1 then you would have to use the DateTime.Parse method and catch the FormatException when your input is invalid. Alan.
Thanks Alan, but that doesn't gives me solution to my problem. If the date is invalid, i want the row entry to be message like this : "Invalid Date" what shud be done in order to achieve this?
-
Thanks Alan, but that doesn't gives me solution to my problem. If the date is invalid, i want the row entry to be message like this : "Invalid Date" what shud be done in order to achieve this?
Something like this then?
try {
dtDate = DateTime.Parse(stringDate);
if (dtDate > startDate && dtDate < endDate) {
row["NEW_DATE_FIELD"] = stringDate;
myTable.ImportRow(row);
}
} catch (FormatException) {
row["NEW_DATE_FIELD"] = "Invalid Date";
myTable.ImportRow(row);
}Alan.