Bug #23538 Exception thrown when GetSchemaTable is called and "fields" is null.
Submitted: 23 Oct 2006 2:21 Modified: 29 Oct 2006 4:50
Reporter: Ludovic Vaugeois-Pepin Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:1.0.8 RC, 5.0.1 OS:Microsoft Windows (Windows XP)
Assigned to: CPU Architecture:Any

[23 Oct 2006 2:21] Ludovic Vaugeois-Pepin
Description:
Please see the "How to repeat".

How to repeat:
Call a stored procedure that does not return a result set with the ExecuteReader function of a MySqlCommand.

ex.:
dr = mysqlCommand.ExecuteReader();

Then call the GetSchemaTable function of the DataReader.

ex.:
dt= dr.GetSchemaTable();

You will then get a "Object reference not set to an instance of an object." error because the 'fields' object in the GetSchemaTable is undefined (see the fix to see why there is an error at that point).

Suggested fix:
--- MySqlClient.orig\datareader.cs	2006-10-18 09:43:44.000000000 -0400
+++ MySqlClient\datareader.cs	2006-10-22 21:45:23.432500700 -0400
@@ -472,7 +472,7 @@
 			// Only Results from SQL SELECT Queries 
 			// get a DataTable for schema of the result
 			// otherwise, DataTable is null reference
-			if (fields.Length == 0) return null;
+			if (fields == null || fields.Length == 0) return null;
 
 			DataTable dataTableSchema = new DataTable("SchemaTable");
[23 Oct 2006 8:31] Tonci Grgin
Hi Ludovic and thanks for your problem report.
Verified as described by reporter:

            //DUMMY PROC
            // DELIMITER //
            // CREATE PROCEDURE `test`.`testBug23538`(p INT) SET @val1 = p //
            // DELIMITER ;

            System.Data.DataTable table = null;

            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = "DataSource=localhost;Database=test;UserID=root;Password=;PORT=3306";
            conn.Open();

            MySqlCommand command = new MySqlCommand();
            command.Connection = (MySqlConnection)conn;

            command.CommandText = "testBug23538";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();
            command.Parameters.Add("p",MySqlDbType.Int32);
            command.Parameters[0].Direction = ParameterDirection.Input;
            command.Parameters[0].Value = 105;

            using (MySqlDataReader reader = command.ExecuteReader())
            {
                table = reader.GetSchemaTable();

                foreach (DataRow row in table.Rows)
                {
                    Console.WriteLine("Column");
                    foreach (DataColumn column in table.Columns)
                    {
                        Console.WriteLine(column.Caption + ": " + row[column]);
                    }
                }

            }

            MessageBox.Show("Ready");
            command.Dispose();
            conn.Close();
[23 Oct 2006 16:58] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/14200
[23 Oct 2006 16:59] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/14201
[29 Oct 2006 4:50] MC Brown
A note has been added to the 5.0.2 changelog