Bug #93370 MySqlParameterCollection.Add precondition check isn't consistent
Submitted: 27 Nov 2018 17:20 Modified: 28 Nov 2018 6:12
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.13 OS:Microsoft Windows (10)
Assigned to: CPU Architecture:Other (x64)

[27 Nov 2018 17:20] Bradley Grainger
Description:
MySqlParameterCollection.Add checks if a named parameter has already been added, but it throws inconsistently based on the order in which parameters are added. For example, assume that `cmd` is a MySqlCommand object.

cmd.Parameters.AddWithValue("foo", 1);
cmd.Parameters.AddWithValue("foo", 2);
^ throws MySqlException: Parameter 'foo' has already been defined.

cmd.Parameters.AddWithValue("foo", 1);
cmd.Parameters.AddWithValue("@foo", 2);
^ throws MySqlException: Parameter '@foo' has already been defined.

cmd.Parameters.AddWithValue("@foo", 1);
cmd.Parameters.AddWithValue("@foo", 2);
^ throws MySqlException: Parameter '@foo' has already been defined.

cmd.Parameters.AddWithValue("@foo", 1);
cmd.Parameters.AddWithValue("foo", 2);
^ doesn't throw

The last example should be consistent with the first three and throw a MySqlException that "Parameter 'foo' has already been defined."

How to repeat:
Run the following C# code:

using (var connection = new MySqlConnection("..."))
{
	connection.Open();
	using (var cmd = connection.CreateCommand())
	{
		cmd.Parameters.AddWithValue("@foo", 1);
		cmd.Parameters.AddWithValue("foo", 2);
		// expect exception to be thrown but it isn't
	}
}
[28 Nov 2018 6:12] Umesh Shastry
Hello Bradley,

Thank you for the report and test case.
Observed with VS 2017 (C#.Net) and Connector/NET 8.0.13 version.

regards,
Umesh