Bug #25013 Return Value parameter not found
Submitted: 12 Dec 2006 15:44 Modified: 3 Jan 2007 10:57
Reporter: Chris Sinclair Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.0.2 OS:Windows (Windows 2000)
Assigned to: CPU Architecture:Any

[12 Dec 2006 15:44] Chris Sinclair
Description:
When adding parameter objects to a command object, if the parameter direction is set to ReturnValue before the parameter is added to the command object then when the command is executed it throws an error "Parameter '<Parameter Name>' not found in the collection."

If the parameter direction is set after it has been added to the command then it works as expected.

This is being used with:

Windows 2000 Professional
MySql Server 5.0
Connector/Net 5.0.2
.Net Framework 2.0
Visual Studio 2005

How to repeat:
The following code recreates the problem.

execSQL("CREATE FUNCTION fnTest(valin int) RETURNS INT " +
" LANGUAGE SQL DETERMINISTIC BEGIN return valin * 2; END"); 
MySqlCommand cmd = new MySqlCommand("fnTest", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("?valin", 22);

MySqlParameter prm = new MySqlParameter();
prm.ParameterName = "?retval";
prm.DbType = DbType.Int32;

//Fails if you set the direction here
prm.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(prm);

//Works if you set the direction here
//prm.Direction = ParameterDirection.ReturnValue;

cmd.ExecuteNonQuery();
Assert.AreEqual(44, cmd.Parameters[1].Value);
[14 Dec 2006 13:40] Tonci Grgin
Hi Chris and thanks for great problem report.
Verified as described by reporter. To repeat one needs a function with at least 2 parameters, 2nd one having ParameterDirection ReturnValue.

parameter_collection.cs
		private MySqlParameter AddReturnParameter(MySqlParameter value)
		{
			for (int i = 0; i < items.Count; i++)
			{
				MySqlParameter p = (MySqlParameter)items[i];
				if (p.Direction != ParameterDirection.ReturnValue) continue;
				items[i] = value;
				return value;
			}
			items.Add(value);
			return value;
		}
for (int i = 0; i < items.Count; i++) relies on parameter already being added.

Tested on latest SVN sources.
[14 Dec 2006 18:57] 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/16978
[14 Dec 2006 18:59] Reggie Burnett
Fixed in 1.0.9 and 5.0.3
[14 Dec 2006 19:00] 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/16979
[3 Jan 2007 10:57] MC Brown
A note has been added to the 1.0.9 and 5.0.3 changelogs.