-
February 21st, 2011, 01:01 AM
#1
DateTime type
I have defined a constant:
public static DateTime NullDateTime = DateTime.MinValue;
There is a field/property in my class:
public DateTime CheckFeedTime {get; set;}
I have a function:
protected static DateTime GetDateTime(DataRow row, string columnName)
{
return (row[columnName] != DBNull.Value) ?
Convert.ToDateTime(row[columnName]) : NullDateTime;
}
In the source code to run:
CheckFeedTime = GetDateTime(row, "StationFeedTime");
I got an exception:
Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.
The field 'StationFeedTime' from MySQL is time field (instead of DateTime). Is that a problem?
-
February 21st, 2011, 09:47 AM
#2
Re: DateTime type
Shouldn't your function be returning a TimeSpan rather than a DateTime?
Something like this:
Code:
protected static TimeSpan GetDateTime(DataRow row, string columnName)
{
return (row[columnName] != DBNull.Value) ?
Convert.ToDateTime(row[columnName]) : NullDateTime;
}
You could do it using a DateTime if you really wanted to, something like this:
Code:
protected static DateTime GetDateTime(DataRow row, string columnName) {
TimeSpan _timespan = row[columnName] != DBNull.Value ? (TimeSpan)row[columnName] : NullDateTime.TimeOfDay;;
return new DateTime(_timespan.Ticks);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|