Bug #36304 DateTime conversion of '0000-00-00' values
Submitted: 23 Apr 2008 19:09 Modified: 26 Apr 2008 2:27
Reporter: Mads Ravn Email Updates:
Status: Won't fix Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.0.7 OS:Any
Assigned to: CPU Architecture:Any

[23 Apr 2008 19:09] Mads Ravn
Description:
Hi,

Using the MySQL .NET Connector (v. 5.0.7) the code attached will generate an exception. I know about the "Convert Zero DateTime" and/or "Allow Zero DateTime" options, but my point is, that you should be able to retrieve the object (as a MySQLDateTime value?) and deal with it appropriately without relying on a certain connection string option being passed. I would not have a problem if a later cast/conversion to DateTime resulted in the same Exception being thrown (as the code in DateTime IConvertible.ToDateTime would do).

The code will also throw an exeption if you attempt to use "dr.IsDBNull(i)" instead of the indexer as this also relies on field retrival.

How to repeat:
	class Program {
		static void Main(string[] args) {
			using(MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;UID=someone;PWD=something;PORT=3306;"))
			using(MySqlCommand cmd = new MySqlCommand("", con)) {
				cmd.Connection.Open();
				cmd.CommandText = 
@"CREATE TABLE IF NOT EXISTS `test`.`test` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `datetime` DATETIME NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB;";
				cmd.ExecuteNonQuery();

				cmd.CommandText = "INSERT INTO `test`.`test`(datetime) VALUES ('0000-00-00 00:00:00')";
				cmd.ExecuteNonQuery();

				cmd.CommandText = "SELECT * FROM `test`.`test`";
				using(IDataReader dr = cmd.ExecuteReader()) {
					while(dr.Read()) {
						for(int i = 0; i < dr.FieldCount; i++) {
							object o = dr[i];
						}
					}
				}
			}
		}
	}

Suggested fix:
Modify code in GetValue from

				if (!dt.IsValidDateTime && connection.Settings.ConvertZeroDateTime)
					return DateTime.MinValue;
				else if (connection.Settings.AllowZeroDateTime)
					return val;
				else
					return dt.GetDateTime();

to

				if(!dt.IsValidDateTime) 
				{
					if(connection.Settings.ConvertZeroDateTime)
						return DateTime.MinValue;
					else
						return val;
				}
				else
				{
					return dt.GetDateTime();
				}
[26 Apr 2008 2:13] Reggie Burnett
I am marking this as verified though it is really a feature request that we are accepting for 5.2
[26 Apr 2008 2:27] Reggie Burnett
After more consideration I have decided not to fix this bug.  The whole point of the allow zero datetime option is to remind the developer that using zero date time values is not best practice.  My recommendation would be to not use zero date time values.