Bug #11450 Connector/Net, current database and stored procedures
Submitted: 20 Jun 2005 9:57 Modified: 20 Jun 2005 21:31
Reporter: Csaba Hatvany Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:MySQL Connector/Net 1.0.4 OS:Microsoft Windows (Windows XP Professional SP2)
Assigned to: Reggie Burnett

[20 Jun 2005 9:57] Csaba Hatvany
Description:
I'm testing stored procedures with 5.0.7.-beta-nt using MySQL Connector/Net 1.0.4 on Windows XP Professional SP2

When you call a stored procedure, the command object makes an additional SELECT call to MySql to determine the parameters of the stored procedure.  That SELECT call is build in the private string GetParameterList( string spName ) function of the internal class StoredProcedure (see module StoredProcedure.cs in the source of MySQL.Data.dll) as follows:

string sql = String.Format("SELECT param_list FROM  mysql.proc WHERE db=_latin1 {0}db AND name=_latin1 {0}name", connection.ParameterMarker);

The db parameter of the subsequently constructed

MySqlCommand cmd = new MySqlCommand(sql, connection);

is taken from the connection object and added to the parameter list of the command:

cmd.Parameters.Add("db", connection.Database);

When the connection object was created without specifying the database parameter, i.e. when connection.Database above is an empty string, you get an

ERROR: Exception trying to retrieve parameter info for sp1: Invalid attempt to access a field before calling Read()

How to repeat:
1.	Connect with Connector/NET to MySQL without specifying a starting database
2.	Change with “USE db” to an existing database
3.	Call an existing sp stored procedure for the current db database
		MySqlCommand cmd = new MySqlCommand("sp", conn);
	cmd.CommandType = CommandType.StoredProcedure;
4.	Add parameters if needed
5.	Execute the stored procedure with
	cmd.ExecuteNonQuery();
6.	You will get the mentioned error

Suggested fix:
In the private string GetParameterList( string spName ) function of the internal class StoredProcedure (see module StoredProcedure.cs in the source of MySQL.Data.dll) I suggest to get the current database name for the db parameter not from the connection string/object, but from an additional “SELECT DATABASE()” query.
[20 Jun 2005 18:10] 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:

This fix will appear in 1.0.5 and all 2.0 releases
[20 Jun 2005 21:31] Mike Hillyer
Documented in 1.0.5 and 2.0.0 changelog (changelog.xml).