Bug #58652 ExecuteReader throws NullReferenceException when using CommandBehavior.Close
Submitted: 2 Dec 2010 10:32 Modified: 7 Jan 2011 15:39
Reporter: Jaco Erasmus Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.3.5 OS:Windows
Assigned to: Reggie Burnett CPU Architecture:Any
Tags: CommandBehavior, ExecuteReader, MySqlCommand, NullReferenceException

[2 Dec 2010 10:32] Jaco Erasmus
Description:
MySqlCommand.ExecuteReader(CommandBehavior) throws a NullReferenceException when being called with CommandBehavior.CloseConnection if the SQL statement has an error or is invalid, e.g. Invalid column name.

Instead it should throw a MySqlException with the correct underlying error message (e.g. Unknown column 'InvalidColumn' in 'field list'), as is the case when calling MySqlCommand.ExecuteReader().

StackTrace:
===========
at MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()
at MySql.Data.MySqlClient.MySqlConnection.Abort()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 
at BugTests.ObjectReferenceTests.ExecuteReader_InvalidColumnName()

How to repeat:
1.) Create test table: CREATE TABLE Test (Id int NOT NULL, Name VARCHAR(100))
2.) Execute select statement with invalid column name (as in code below)
3.) Expect MySqlException with message "Unknown column 'InvalidColumn' in 'field list'", instead receives NullReferenceException

try
{
  string connStr = "CONNECTION STRING HERE";
  using (MySqlConnection conn = new MySqlConnection(connStr))
  {
    conn.Open();

    // Select invalid column
    string sql = "SELECT Id, Name, InvalidColumn FROM Test";
    using (MySqlCommand cmd = new MySqlCommand(sql, conn))
    {
      using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
      {
        Assert.AreEqual(0, reader.RecordsAffected);
      }
    }
  }
}
catch (Exception ex)
{
  throw ex;
}
[3 Dec 2010 17:05] Tonci Grgin
Hi Jaco and thanks for your report.

Verified just as described with following test case:
    string s = "Server=opensol;Database=mysql;Uid=**;pwd=**;logging=true";
            using (MySqlConnection conn = new MySqlConnection(s))
            {
                conn.Open();

                // Select invalid column
                string sql = "SELECT User, Host, Pswrd FROM user";
                using (MySqlCommand cmd = new MySqlCommand(sql, conn))
                {
                    using (MySqlDataReader reader =
                        cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        Assert.AreEqual(0, reader.RecordsAffected);
                    }
                }
            }

Problem is in command.cs, right about line 469 where this case is not handled and later on original exception gets overwritten with new one.
[14 Dec 2010 15:28] Reggie Burnett
Fixed in 6.0.8, 6.1.6, 6.2.5, 6.3.6+
[7 Jan 2011 15:39] Tony Bedford
An entry has been added to the 6.0.8, 6.1.6, 6.2.5, 6.3.6 changelogs:

MySqlCommand.ExecuteReader(CommandBehavior) threw a NullReferenceException when being called with CommandBehavior.CloseConnection, if the SQL statement contained a syntax error, or contained invalid data such as an invalid column name.