Bug #33754 GetString can't handle nulls
Submitted: 9 Jan 2008 2:32 Modified: 9 Jan 2008 19:32
Reporter: Jared S (Silver Quality Contributor) Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:5.1.4 OS:Windows (Any)
Assigned to: CPU Architecture:Any
Tags: getstring, nulls

[9 Jan 2008 2:32] Jared S
Description:
.GetString() bombs out on nulls. 

How to repeat:
private MySql.Data.MySqlClient.MySqlDataReader DTR0; //handles NULLs alot better
private MySql.Data.MySqlClient.MySqlConnection CON0;
private MySql.Data.MySqlClient.MySqlCommand CMD0;

try
{
CON0 = new MySql.Data.MySqlClient.MySqlConnection();
CON0.ConnectionString = "server=localhost;database=classic;uid=root;pwd=;port=3306";
CON0.Open();
}
catch (Exception ex)
{
System.Diagnostics.Debugger.Break();
}

CMD0 = new MySql.Data.MySqlClient.MySqlCommand("select null as Ex1", CON0);
DTR0 = CMD0.ExecuteReader();

DTR0.Read();
Trace.WriteLine(DTR0.GetString("Ex1")); //Errors here
DTR0.Close();

Suggested fix:
I have personal thoughts on this issue..
1. Don't deploy MySQL.data.dll with errors
2. It is consider OK to return '' instead of DBNull object
[9 Jan 2008 3:15] Jared S
+S1 blocking issue
[9 Jan 2008 15:45] Reggie Burnett
This is not a bug.  The behavior is identical to SqlClient and is documented in the help file.  It is designed this way to be consistent with the other GetXXX methods.  It is impossible for methods like GetInt32 and GetFloat to return a null value to indicate that the column value is null so it would be inconsistent to allow GetString to do so.  Also, returning '' to indicate a null value is a very bad idea since empty string is a non-null value and is a valid value for a string column.  

The correct behavior is the current behavior and that requires the user to either call GetValue (which will return DbNull.Value if the value is null) or call IsDBNull on the column to first check if the column is null.
[9 Jan 2008 19:32] Jared S
Thank you, I will work around issue in code.