Bug #18391 Better error handling for the .NET class "MySqlCommand" needed.
Submitted: 21 Mar 2006 14:19 Modified: 16 Oct 2006 17:33
Reporter: Ola Söderlund Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:1.0.7 for .NET 1.1 OS:Windows (Windows XP)
Assigned to: CPU Architecture:Any

[21 Mar 2006 14:19] Ola Söderlund
Description:
"MySQL Connector Net 1.0.7" för ".NET 1.1" 

With the code below the i.e. an empty string to the prepare() method will run in to problems since there is no MySQL exeption handling the error.
  
MySql.Data.MySqlClient.MySqlCommand cmd;
cmd = new MySql.Data.MySqlClient.MySqlCommand(); 
cmd.Connection = conn; 
cmd.CommandText = ""; 
cmd.Prepare(); 

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in mscorlib.dll 
Additional information: The index is outside the ranges fo the matrix. 

(The additional information above is translated from swedish and migth not be fully accurate) 

How to repeat:
See above.

Suggested fix:
Find the fault and throw an exeption (new or existing).
[7 Apr 2006 9:19] Tonci Grgin
Thanks for your problem report. Verified as described by reporter on NET 1.1 and 2.0 with latest source (command.cs):

		public void Prepare()
		{
			if (connection == null)
				throw new InvalidOperationException(Resources.GetString("ConnectionNotSet"));
			if (connection.State != ConnectionState.Open)
				throw new InvalidOperationException(Resources.GetString("ConnectionNotOpen"));
			if (! connection.driver.Version.isAtLeast( 4,1,0)) 
				return;

			// strip out names from parameter markers
			string psSQL = CommandText;

			if (CommandType == CommandType.StoredProcedure)
			{
				if (storedProcedure == null)
					storedProcedure = new StoredProcedure(connection);
				psSQL = storedProcedure.Prepare(this);
			}
from >>			psSQL = PrepareCommandText(psSQL);

			// ask our connection to send the prepare command
			preparedStatement = connection.driver.Prepare(psSQL, (string[])parameterMap.ToArray(typeof(string)));
		}

		private string PrepareCommandText(string text)
		{
			StringBuilder	newSQL = new StringBuilder();

			// tokenize the sql first
			ArrayList tokens = TokenizeSql(text);
			parameterMap.Clear();

			foreach (string token in tokens)
			{
>>Exception here		if ( token[0] != parameters.ParameterMarker )
					newSQL.Append( token );
				else
				{
					parameterMap.Add( token );
					newSQL.Append( parameters.ParameterMarker );
				}
			}

			return newSQL.ToString();
		}
I'm leaving Severity S3 since workaround exists.
[26 Sep 2006 19:54] 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/12566
[26 Sep 2006 19:55] 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/12567
[26 Sep 2006 19:55] Reggie Burnett
Fixed in 1.0.8 and 5.0.1
[16 Oct 2006 17:33] MC Brown
A note has been added to the 5.0.1 and 1.0.8 changelogs.