tags:

views:

60

answers:

5

I'm reading a record from a DB2 database via ODBC connection. The data is populated into an ODBCDataReader. As I'm going through my foreach loop I'm running into problems trying to parse the multiple different DateTime fields.

Some of the fields are null, some have null date time value (9999-12-31 24:00:00.000000) and some have valid date time values (2010-07-09 20:43:32.037234).

I've tried doing something like this to catch null date time errors:

if (!dr[dbFieldName].Equals(DBNull.Value))
{
  if (dr.GetDate(dr.GetOrdinal(dbFieldName)).Equals(DateTime.Parse("9999-12-31 24:00:00.000000")))
  {
    fieldValues[tag] = "";
  }
  else
  {
    strValue = dr.GetDate(dr.GetOrdinal(dbFieldName)).ToString("s");
    fieldValues[tag] = strValue.Trim();
  }
}

The GetType().Name != "DBNull" seems to work for catching null values. However the next if statement throws an ArgumentOutOfRangeException error. This appears to happen on fields with the 9999-12-31 24:00:00.000000 values.

Is there a way to properly parse this? It seems like any way I try to evaluate these null date time fields a error is thrown.

A: 

Two suggestions:

1) Use the DBNull.Value object to check for null.
2) Use DateTime.MaxValue to check 12/31/999 24:00:00.

Hopefully that will help you get closer to finding the problem.

Jerod Houghtelling
Updated my code with suggestion #1. DateTime.MaxValue is readonly and says that 23:59.00 is the maximum as Raj Kaimal points out above.
Neil
A: 

In addition to Jerod's comment.

DateTime.Parse("9999-12-31 23:59:00.000000") is valid
DateTime.Parse("9999-12-31 24:00:00.000000") is invalid

update....

The problem is 24:00 is not considered a valid time in the 24 hour clock (regardless of date). You may be able to work around that by using

    DateTime.ParseExact("9999-12-31 24:00:00.000000", "yyyy-MM-dd 24:mm:ss.ffffff",
System.Globalization.CultureInfo.InvariantCulture); 

after a DateTime.TryParse method. If TryParse succeeds, you have a valid date. If it returns false, the above statemen tis executed.

Raj Kaimal
The question is would now be, since "9999-12-31 24:00:00.000000" is invalid as a DateTime value, and apparently is what my ODBCDataReader contains, how would I get that value out?
Neil
A: 

Wrap the IF/ELSE in a TRY/CATCH. It's not considered good form, but catching the exception is gauranteed to work.

kirk.burleson
This is the route I went with originally, but my Output/Debug window was being flooded with messages about the error being caught. I'd like to try to find a solution that doesn't involve this method if possible.
Neil
A: 

What about DateTime.TryParse?

jdv
A: 

Try this:

Date dt;

// DBNull.Value.ToString() results in an empty string

if (Date.TryParse(dr[dbFieldName].ToString(), dt)) {
fieldValues[tag] = dt.ToString("s");
} else {
fieldValues[tag] = "";
}
jmoreno
This is still throwing ArgumentOutOfRangeException. It appears anything that touches dr[dbFieldName] when it contains a time value of 24:00:00.000000 throws this error.
Neil
Have you stepped through it make sure that it is throwing the exception inside the if statement?
jmoreno
Yes. As soon as it evaluates any of these if statements it throws the Argument OutOfRangeException.if (dr[dbFieldName].Equals(DateTime.Parse("12/31/9999 12:00:00 AM")))//if (!dr[dbFieldName].Equals(DBNull.Value))//if (dr[dbFieldName].GetType().Name != "DBNull")
Neil
Are you creating the columns or are you letting the query create them for you?
jmoreno
I'm passing the ODBCCommand the SQL query, then doing the ExecuteReader() and passing that into an ODBCDataReader. So I guess the DataReader is creating the columns for me.
Neil
OK. Sounds to me like you've run into a bug in the ODBCDataReader, the solution would be to change your query -- cast the date/time that is giving you trouble into something else, and then deal with that. This will make your query slightly more complex, but you can guarantee that your code will be able to handle it.
jmoreno