Bug #64551 Connector Throws NullReferenceException On ExecuteReader
Submitted: 5 Mar 2012 15:48 Modified: 23 Mar 2012 20:23
Reporter: John Carroll Email Updates:
Status: Can't repeat Impact on me:
Category:Connector / NET Severity:S2 (Serious)
Version:6.4.4 OS:Windows
Assigned to: Fernando Gonzalez.Sanchez CPU Architecture:Any

[5 Mar 2012 15:48] John Carroll
If the value of a command parameter is invalid (eg. parameter expects an integer and the value is a string which cannot be converted to integer) mscorlib correctly throws a FormatException but then MySql throws a NullReferenceException making any information in the format exception unavailable. 

How to repeat:
Create a command with an Int Type and set it value to a String then do a command.ExecuteReader(). The output window shows a FormatException followed by a NullReference exception.

Suggested fix:
Ensure the FormatException is passed to the calling routine.
[5 Mar 2012 16:17] Valeriy Kravchuk
Based on version, this is probably about Connector/Net. Please, correct me if this my assumption is wrong.
[5 Mar 2012 19:48] John Carroll
You are correct. Thanx for the quick response.
[22 Mar 2012 22:55] Fernando Gonzalez.Sanchez
Thanks John for your report,

However, I need you to provide more details (like script & code sample), because I wasn't able to reproduce as such.

This is what I tried (in Connector/NET 6.4.4, MySql 5.5.16):

MySqlConnection con = new MySqlConnection( ... 
string sql = @"drop function if exists MyTwice; create function MyTwice( val int ) returns int begin return val * 2; end;";
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.CommandText = "MyTwice";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("val", MySqlDbType.Int32);
cmd.Parameters.Add("@p", MySqlDbType.Int32);
cmd.Parameters[1].Direction = ParameterDirection.ReturnValue;
cmd.Parameters[0].Value = "a"; // 5;

I just get the Format Exception:

Test 'MySql.Data.MySqlClient.Tests.CommandTests.InvalidCastBadMessage' failed: System.FormatException : Input string was not in a correct format.
	at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
	at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
	at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
	at System.Convert.ToInt32(Object value)
	Types\MySqlInt32.cs(95,0): at MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket packet, Boolean binary, Object val, Int32 length)
	parameter.cs(412,0): at MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings)
	Statement.cs(225,0): at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName)
	Statement.cs(177,0): at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)
	Statement.cs(109,0): at MySql.Data.MySqlClient.Statement.BindParameters()
	Statement.cs(86,0): at MySql.Data.MySqlClient.Statement.Execute()
	PreparableStatement.cs(123,0): at MySql.Data.MySqlClient.PreparableStatement.Execute()
	command.cs(481,0): at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
	command.cs(399,0): at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
	command.cs(342,0): at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()

[23 Mar 2012 0:55] John Carroll
Apologies for putting you through this effort. Had someone check by project. It seems I had out of date version of Connector/Net. Loaded latest version. Output as expected. Thanx for your efforts.

John Carroll
[23 Mar 2012 20:23] Fernando Gonzalez.Sanchez
No problem John, I'm closing this as "cannot repeat", however if you find a case when it actually happens, feel free to reopen.