Date not recognized - strange behaviour in DateTime.Parse

  • Thread starter Thread starter Manoj Misran
  • Start date Start date
M

Manoj Misran

Dear Gurus,
I have a strange problem. Following code is working with no problems
on 10 different computers but only on one specific Win2K PC, it is not
working for a specific user ID. The problem is very strange. Look at
the code first:

DateTime myCreationDate;
object myDate = myOleDbReader.GetDateTime(myOleDbReader.GetOrdinal("CreationDateTime"));
if (myDate!=null)
{
myCreationDate = DateTime.Parse(myDate.ToString());
}
else
{
myCreationDate = DateTime.MinValue;
}

Now this code snippet gives me an Exception on "DateTime.Parse"
function that says "String was not recognized as a valid DateTime".
But it works on 10 of the 11 Computers and even on this 11th computer
it DOES work with all other user IDs except only one user. Everything
is same.

I did some research and found an interesting thing. The date value
stored in Database is "4/2/2003 6:10:00 PM". Now for all the working
users, the myReader.GetDateTime() function gives me the same string
and it parses correctly but on that one PC for that one user what I
get is "4/2/2003 18:10:00 PM" that gets an exception while parsing. I
don't know why it is showing 18 instead of 6 just for that one user on
this specific PC. Is there any setting that I need to check for this
user.

PS: This user is a network domain user and I checked with other domain
users, it works perfectly allright with other domain and local users.

Thanks in advance.

-Manoj Misran
(e-mail address removed)
 
Manoj Misran said:
I did some research and found an interesting thing. The date value
stored in Database is "4/2/2003 6:10:00 PM". Now for all the working
users, the myReader.GetDateTime() function gives me the same string
and it parses correctly but on that one PC for that one user what I
get is "4/2/2003 18:10:00 PM" that gets an exception while parsing. I
don't know why it is showing 18 instead of 6 just for that one user on
this specific PC. Is there any setting that I need to check for this
user.

I think the first thing to do is find out what that user's regional
settings are - not just the locale, but what he's got his date/time
input settings to be.

However, I'm not sure why you're converting the DateTime into a string
and then back again in the first place. What type is GetDateTime
declared to return? Are you sure it ever actually returns null?
 
Thank ou very much Jon for the Reply.
YES! there was a typo in the regional settings for that user. The time
format was "H:mm:ss tt" in stead of "h:mm:ss tt". Because of capital H
it was displaying hour in 24 hours format which would have been OK had
there been no "tt" in the end. The DateTime.Parse treat this time
"18:10:00 PM" as invalid format though technically it is right with a
bit of redundancy. I changed Caps H to lowercase and it worked fine.

Now the next. Again I made a typing mistake in the Post. My
appologies. Actually GetDateTime is GetValue and 'Null' is 'DbNull'.
If there is a null value in the database for a date field,
getdatetime() gives exception because DateTime is a value type.
Because of that I always receive the value in 'object' type using
getValue() and then check for dbNull to parse the value or assign
MinValue. The corrected code in my program is as follows:

DateTime myCreationDate;
object myDate = myOleDbReader.GetValue(myOleDbReader.GetOrdinal("CreationDateTime"));
if (myDate!=System.DbNull)
{
myCreationDate = DateTime.Parse(myDate.ToString());
}
else
{
myCreationDate = DateTime.MinValue;
}

Is there any better way to do it?

Thanks again for the help

-Manoj Misran
 
Manoj Misran said:
Thank ou very much Jon for the Reply.
YES! there was a typo in the regional settings for that user. The time
format was "H:mm:ss tt" in stead of "h:mm:ss tt". Because of capital H
it was displaying hour in 24 hours format which would have been OK had
there been no "tt" in the end. The DateTime.Parse treat this time
"18:10:00 PM" as invalid format though technically it is right with a
bit of redundancy. I changed Caps H to lowercase and it worked fine.
Right.

Now the next. Again I made a typing mistake in the Post. My
appologies. Actually GetDateTime is GetValue and 'Null' is 'DbNull'.
If there is a null value in the database for a date field,
getdatetime() gives exception because DateTime is a value type.

Right. After getting the value and checking whether or not it's null, I
*think* you may well be able to just cast to DateTime.

Alternatively, use IsDbNull first, and then use GetDateTime if it's not
null. Certainly I'd be surprised if you really needed to convert it to
a string and then parse it.
 
Right. After getting the value and checking whether or not it's null, I
*think* you may well be able to just cast to DateTime.

Alternatively, use IsDbNull first, and then use GetDateTime if it's not
null. Certainly I'd be surprised if you really needed to convert it to
a string and then parse it.

I see your point. Thats a better way to do it. Thanks a bunch.
 
Back
Top