Bug #11542 Call to Stored Procedure throws exception when SP has no arguments
Submitted: 24 Jun 2005 7:37 Modified: 25 Jun 2005 18:40
Reporter: Steve Terepin Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:1.0.4 OS:Microsoft Windows (Win XP SP2)
Assigned to: Reggie Burnett

[24 Jun 2005 7:37] Steve Terepin
Description:
Calls to a Stored Procedure that takes no arguments fail, with an exception that reports "Invalid attempt to access a field before calling Read()".

How to repeat:
      try
      {
        string connectionString = (
          "SERVER=localhost;"
        + "DATABASE=X_SqlTestDatabase_01;"
        + "User ID=root;"
        + "PASSWORD=password"
        ) ;
        using ( System.Data.IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString) )
        {
          connection.Open() ;
          System.Data.IDbCommand command = new MySql.Data.MySqlClient.MySqlCommand() ;
          command.Connection = connection ;
          command.CommandType = System.Data.CommandType.StoredProcedure ;
          command.CommandText = "PurgeAllTables" ;
          command.ExecuteNonQuery() ;
        }
      }
      catch ( System.Exception x )
      {
        string message = x.Message ;
      }

... where the 'PurgeAllTables' stored procedure is defined as follows :

DROP PROCEDURE IF EXISTS `PurgeAllTables` ;
DELIMITER `$`;
CREATE PROCEDURE `PurgeAllTables` ( )
BEGIN
END$
DELIMITER `;`$

Suggested fix:
The problem is in MySql.Data.MySqlClient.StoredProcedure.GetParameterList().

try 
{
  reader = cmd.ExecuteReader();
  reader.Read();                          <= returns false when there are no args
  return reader.GetString(0);
}

Change this to :

try 
{
  reader = cmd.ExecuteReader();
  if ( reader.Read() )
    return reader.GetString(0);
  else
  {
    // The stored procedure defines no arguments
    return null ;
  }
}
[24 Jun 2005 20:25] Reggie Burnett
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

I wasn't able to reproduce this per se.  It might have been related to another bug report where if you had white space in the parameter list, it would fail.  I applied your suggestion though to return null if .Read() returns false.  I also cleaned up some other related code.

This will be in 1.0.5
[25 Jun 2005 18:40] Mike Hillyer
Documented in 1.0.5 changelog.