Description:
Recent versions of Connector/NET throw an exception for queries that return a field that contains a null when the data type is System.Guid. The typical behavior for Connector/NET is to return a System.Guid when the column is created as char(36). If the field contains a null, Connector/NET normally returns a System.DBNull object. Since Connector/NET 8.0.22 the result has been an exception. I believe the problem is entirely in the connector, but so you know I’m testing with MySQL Community Server 5.6.26, InnoDb. It’s easy to demonstrate the problem.
How to repeat:
Create a table and insert a couple rows:
CREATE TABLE entities (
id int NOT NULL AUTO_INCREMENT,
notnullguid char(36) NOT NULL,
nullguid char(36) DEFAULT NULL,
PRIMARY KEY (id)
)
insert into entities (notnullguid,nullguid) values ('275d7683-0cc8-44ea-8e64-29872d10fc23','1fcb5dbf-d107-4783-8e9c-e05786e40410');
insert into entities (notnullguid) values ('cf81cf52-9426-43f3-aaa3-33a425646c07');
The table looks like this:
+----+--------------------------------------+--------------------------------------+
| id | notnullguid | nullguid |
+----+--------------------------------------+--------------------------------------+
| 1 | 275d7683-0cc8-44ea-8e64-29872d10fc23 | 1fcb5dbf-d107-4783-8e9c-e05786e40410 |
| 2 | cf81cf52-9426-43f3-aaa3-33a425646c07 | (null) |
+----+--------------------------------------+--------------------------------------+
Now some queries:
select * from entities where id=1 <-- THIS QUERY WORKS
select * from entities where id=2 <-- THIS QUERY FAILS WITH AN EXCEPTION STARTING WITH Connector/NET 8.0.22.
Example code:
MySqlConnection sql_con = new MySqlConnection(connect_str);
sql_con.Open();
MySqlCommand cmd = sql_con.CreateCommand();
cmd.CommandText = "select * from entities where id=2";
MySqlDataReader reader = cmd.ExecuteReader();
reader.Read(); // <-- exception
Exception ToString():
System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: count
at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
at MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject)
at MySql.Data.MySqlClient.ResultSet.ReadColumnData(Boolean outputParms)
at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlDataReader.Read()
at MySQLTest.Form1.button1_Click(Object sender, EventArgs e)
Description: Recent versions of Connector/NET throw an exception for queries that return a field that contains a null when the data type is System.Guid. The typical behavior for Connector/NET is to return a System.Guid when the column is created as char(36). If the field contains a null, Connector/NET normally returns a System.DBNull object. Since Connector/NET 8.0.22 the result has been an exception. I believe the problem is entirely in the connector, but so you know I’m testing with MySQL Community Server 5.6.26, InnoDb. It’s easy to demonstrate the problem. How to repeat: Create a table and insert a couple rows: CREATE TABLE entities ( id int NOT NULL AUTO_INCREMENT, notnullguid char(36) NOT NULL, nullguid char(36) DEFAULT NULL, PRIMARY KEY (id) ) insert into entities (notnullguid,nullguid) values ('275d7683-0cc8-44ea-8e64-29872d10fc23','1fcb5dbf-d107-4783-8e9c-e05786e40410'); insert into entities (notnullguid) values ('cf81cf52-9426-43f3-aaa3-33a425646c07'); The table looks like this: +----+--------------------------------------+--------------------------------------+ | id | notnullguid | nullguid | +----+--------------------------------------+--------------------------------------+ | 1 | 275d7683-0cc8-44ea-8e64-29872d10fc23 | 1fcb5dbf-d107-4783-8e9c-e05786e40410 | | 2 | cf81cf52-9426-43f3-aaa3-33a425646c07 | (null) | +----+--------------------------------------+--------------------------------------+ Now some queries: select * from entities where id=1 <-- THIS QUERY WORKS select * from entities where id=2 <-- THIS QUERY FAILS WITH AN EXCEPTION STARTING WITH Connector/NET 8.0.22. Example code: MySqlConnection sql_con = new MySqlConnection(connect_str); sql_con.Open(); MySqlCommand cmd = sql_con.CreateCommand(); cmd.CommandText = "select * from entities where id=2"; MySqlDataReader reader = cmd.ExecuteReader(); reader.Read(); // <-- exception Exception ToString(): System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: count at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count) at MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject) at MySql.Data.MySqlClient.ResultSet.ReadColumnData(Boolean outputParms) at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlDataReader.Read() at MySQLTest.Form1.button1_Click(Object sender, EventArgs e)