locked
MySQL - CSharp to assign null date time to a class field RRS feed

  • Question

  • User-1697400506 posted

    Using MySQL 5.

    In my SAP.NEt project, defined a constant:

    public static DateTime NullDateTime = DateTime.MinValue;

     

    I have a function:

    protected static DateTime GetDateTime(DataRow row, string columnName)
        {
            return (row[columnName] != DBNull.Value) ?
            Convert.ToDateTime(row[columnName]) :
            Globals.NullDateTime;
        }

    In the source code:

    public DateTime StationFeedTime{ get; set; }

    StationFeedTime = GetDateTime(row, "StationFeedTime");

     

    I ran a SQL to fetch records from MySQL, this field is Time field in MySQL.

    Got an exception inside the function:

    Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.

    Monday, February 21, 2011 12:38 AM

All replies

  • User-1373473781 posted

    System.Nullable<DateTime> NullDate = DateTime.Now;
    try this

    Monday, February 21, 2011 12:59 AM
  • User-1697400506 posted

    Sorry,

     

    where should I put that to?

    Monday, February 21, 2011 1:03 AM
  • User-1373473781 posted

    public static DateTime NullDateTime = DateTime.MinValue;

    here with it

    System.Nullable<DateTime> NullDate = DateTime.Now;

    Monday, February 21, 2011 1:05 AM
  • User-1697400506 posted

    Hi,

     

    I might have missed something.

    I have changed from

    public static DateTime NullDateTime = DateTime.MinValue;

    to

    public static System.Nullable<DateTime> NullDate = DateTime.Now;

     

    I got lots of compilation error in other places unrelated places.

    Monday, February 21, 2011 1:18 AM
  • User-1373473781 posted

    public static System.Nullable<DateTime> NullDate = DateTime.Now;

    it is possibly because of Nullable

    ok only use this much

    public static DateTime NullDate = DateTime.Now;

    Monday, February 21, 2011 1:20 AM
  • User-1697400506 posted

    Hi,

    I changed using the

    public static DateTime NullDateTime = DateTime.Now;

    I still got the same exception:

    Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.

    Monday, February 21, 2011 5:29 PM
  • User-1697400506 posted

    I think I have a clue but not sure why:

    If I commented as this, then no exception happened -

    //FeedTime = GetDateTime(row, "StationFeedTime");
    AirDate = GetDateTime(row, "AirDate");

     

    I think the field "StationFeedTime" in the data base is "Time" (eg. 05:30:00). This table field always have value, then the problem is not about Globals.NullDateTime;

    but

    related to

    Convert.ToDateTime(row[columnName])

     

    Monday, February 21, 2011 5:36 PM
  • User-1697400506 posted

    Hi,

     

    I changed from

    return (row[columnName] != DBNull.Value) ?
            Convert.ToDateTime(row[columnName]) : Globals.NullDateTime;

    to

    return (row[columnName] != DBNull.Value) ?
            Convert.ToDateTime(row[columnName].ToString()) : Globals.NullDateTime;

     

    I have no idea why it works.

     

    Tuesday, March 8, 2011 7:56 PM