CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: DateTime type

  1. #1
    Join Date
    Feb 2011
    Posts
    19

    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?

  2. #2
    Join Date
    Feb 2011
    Location
    NET2.0 / VS2005, .NET3.5 / VS2008, .NET 4.0 / VS2010
    Posts
    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
  •  





Click Here to Expand Forum to Full Width

Featured