Bug #29312 System.FormatException if parameter not found
Submitted: 23 Jun 2007 9:56 Modified: 7 Aug 2007 7:21
Reporter: Felix Lelchuk Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.1.2 OS:Windows
Assigned to: CPU Architecture:Any
Tags: FormatException, Parameter

[23 Jun 2007 9:56] Felix Lelchuk
Description:
Looks like there is a problem when trying to use non-existent parameters.
To make it short: have a look at the following code in Statement.SerializeParameter:

                throw new MySqlException(
                    String.Format(Resources.ParameterMustBeDefined));

In case the parameter is not found a MySqlException is supposed to be
thrown. Unfortunately before this happens a System.FormatException
occurs because Resources.ParameterMustBeDefined contains '{0}' and String.Format
expects a second parameter.

How to repeat:
public void Buggy(MySqlCommand command)
{
  command.Parameters.Clear(); // make sure there are no parameters
  command.CommandText = "SELECT * FROM mysql.user WHERE Host=?host";  // use non-existent parameter '?host'
  command.ExecuteScalar();
}

Suggested fix:
throw new MySqlException(String.Format(Resources.ParameterMustBeDefined), parmName);
[3 Jul 2007 17:08] Felix Lelchuk
Sorry just noticed the suggestion is wrong.
Should be:

throw new MySqlException(String.Format(Resources.ParameterMustBeDefined, parmName));
[10 Jul 2007 15:56] 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/30616
[10 Jul 2007 15:56] 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/30616
[10 Jul 2007 15:57] Reggie Burnett
fixed in 5.0.8 and 5.1.3
[10 Jul 2007 16:01] 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/30617
[10 Jul 2007 16:01] 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/30617
[7 Aug 2007 7:21] MC Brown
A note has been added to the 5.0.8 and 5.1.3 changelogs: 

A FormatException error would be raised if a parameter had not been found, instead of Resources.ParameterMustBeDefined