Bug #101252 Can't query CHAR(36) column containing NULL
Submitted: 20 Oct 2020 20:09 Modified: 27 Jan 2021 18:56
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.22 OS:Windows (10)
Assigned to: CPU Architecture:Any
Tags: regression

[20 Oct 2020 20:09] Bradley Grainger
The column type CHAR(36) is used to represent a GUID in MySQL Server (and MySqlDataReader.GetValue will return a System.Guid value from that column).

Starting in 8.0.22, querying a CHAR(36) column containing NULL values will throw an ArgumentOutOfRangeException.

ArgumentOutOfRangeException: Non-negative number required. (Parameter '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() 

How to repeat:
Run the following C# code:

using var connection = new MySqlConnection("...");

using var command = new MySqlCommand(@"drop table if exists guid_test;
create table guid_test(value char(36));
insert into guid_test(value) values(null);", connection);

command.CommandText = "select value from guid_test;";
using var reader = command.ExecuteReader();
while (reader.Read()) // <-- exception thrown here
	Console.WriteLine(reader.IsDBNull(0)); // expected True

Suggested fix:
Test for 'length != -1' here: https://github.com/mysql/mysql-connector-net/commit/c8b7779b9f98f6985600483a5331d951f40127...
[21 Oct 2020 5:52] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.
Verified as described.

[19 Nov 2020 16:47] Kevin Hoang
When will this bug be fixed? I'm getting the same issue when updating to MySql.Data version 8.0.22.

[27 Jan 2021 18:56] Christine Cole
Posted by developer:
Fixed as of the upcoming MySQL Connector/NET 8.0.24 release, and here's the proposed changelog entry from the documentation team:

An exception was thrown if any CHAR(36) columns containing a NULL value
were referenced in a query. New validation now checks for NULL values when
the MySqlDbType member is Guid.

Thank you for the bug report.
[9 Apr 2021 12:58] MySQL Verification Team
Bug #103259 marked as duplicate of this one.
[8 Dec 2022 23:40] Gustavo Amaya
The error persists despite having scaled the versions, from 8.0.24 to 31 to the present.