Bug #7478 Clone method bug in MySqlCommand
Submitted: 22 Dec 2004 12:04 Modified: 5 Jan 2005 17:55
Reporter: Gilles Bayon Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:1.0.3 OS:Windows (Windows XP)
Assigned to: Reggie Burnett CPU Architecture:Any

[22 Dec 2004 12:04] Gilles Bayon
Description:
If you clone a MySqlCommand whitout a connection null --> bug in the connection set 

How to repeat:
IDbCommand command = new MySqlCommand();
IDbCommand c = ((ICloneable)command ).Clone() as IDbCommand;

Suggested fix:
In MySqlCommand bug in the connection set method line 183
connection = (MySqlConnection)value;
replace it by

++connection = value as MySqlConnection;
++if (connection != null)
++{
  parameters.ParameterMarker = connection.ParameterMarker;
++}

+ idem bug in 
public MySqlCommand(string cmdText, MySqlConnection connection) : this(cmdText)
{
Connection = connection;
++if (connection != null)
++{
 parameters.ParameterMarker = connection.ParameterMarker;
++}
}

Cheers
-Gilles
[5 Jan 2005 17:55] 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
[30 Apr 2005 15:11] Roberto Rabe
In Version 1.0.4...

Cloning a connection also throws an unhandled exception if the original Connection still has the default ConnectionString Hashtable values.  Simple/quick ConnectionTests NUnit test to throw exception:

[Test()]
public void TestConnectionClone()
{
	MySqlConnection c = new MySqlConnection();
	MySqlConnection clone = (MySqlConnection) ((ICloneable)c).Clone();
}

MySql.Data.MySqlClient.Tests.ConnectionTests.TestConnectionClone : System.NullReferenceException : Object reference not set to an instance of an object.

   at MySql.Data.MySqlClient.MySqlConnectionString.CreateConnectionString() in \MySqlClient\ConnectionString.cs:line 300
   at MySql.Data.MySqlClient.MySqlConnectionString.GetConnectionString() in \MySqlClient\ConnectionString.cs:line 261
   at MySql.Data.MySqlClient.MySqlConnection.get_ConnectionString() in \MySqlClient\Connection.cs:line 172
   at MySql.Data.MySqlClient.MySqlConnection.System.ICloneable.Clone() in \MySqlClient\Connection.cs:line 344
   at MySql.Data.MySqlClient.Tests.ConnectionTests.TestConnectionClone() in \testsuite\connectiontests.cs:line 50

ConnectionString.CreateConnectionString() throws exception when looping through keys and gets to "charset" key:

/// <summary>
/// Uses the values in the keyValues hash to create a
/// connection string
/// </summary>
/// <returns></returns>
public string CreateConnectionString()
{
	string cStr = String.Empty;

	Hashtable values = (Hashtable)keyValues.Clone();
	Hashtable defaultValues = GetDefaultValues();

	if (!PersistSecurityInfo && values.Contains("password") )
		values.Remove( "password" );

	// we always return the server key.  It's not needed but 
	// seems weird for it not to be there.
	cStr = "server=" + values["host"] + ";";
	values.Remove("server");

	foreach (string key in values.Keys)
	{
		if (!values[key].Equals( defaultValues[key]))
			cStr += key + "=" + values[key] + ";";
	}

	return cStr;
}

Roberto