Bug #40547 System.FormatException in IMySqlValue.ReadValue
Submitted: 6 Nov 2008 11:52 Modified: 26 Oct 2011 17:47
Reporter: Angelo Abatemarco Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:5.2.3 OS:Any
Assigned to: Reggie Burnett CPU Architecture:Any
Tags: parse

[6 Nov 2008 11:52] Angelo Abatemarco
Description:
The IMySqlValue.ReadValue(...) uses Parse as parsing of numeric types,
namely int/long/uint/ulong/double/decimal.

In some cases I get an exception like this :

System.FormatException: Formato della stringa di input non corretto.
   in System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   in System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   in System.Int32.Parse(String s, IFormatProvider provider)
   in MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.ReadValue(MySqlStream stream, Int64 length, Boolean nullVal) in G:\_MyProjects\DataManager\MySqlClient\Driver\Source\Types\MySqlInt32.cs:riga 109
   in MySql.Data.MySqlClient.NativeDriver.ReadColumnValue(Int32 index, MySqlField field, IMySqlValue valObject) in G:\_MyProjects\DataManager\MySqlClient\Driver\Source\NativeDriver.cs:riga 628
   in MySql.Data.MySqlClient.MySqlDataReader.Read() in G:\_MyProjects\DataManager\MySqlClient\Driver\Source\datareader.cs:riga 891

How to repeat:
This often happens in cases when there are some null fields in a record

Suggested fix:
Here is an example in MySqlInt32.cs:
IMySqlValue IMySqlValue.ReadValue(MySqlStream stream, long length, bool nullVal)
{
    if (nullVal)
      return new MySqlInt32((this as IMySqlValue).MySqlDbType, true);
    if (length == -1)
      return new MySqlInt32((this as IMySqlValue).MySqlDbType,
             stream.ReadInteger(4));
    else
      return new MySqlInt32((this as IMySqlValue).MySqlDbType,
             Int32.Parse(stream.ReadString(length),
             CultureInfo.InvariantCulture));
}

In .Net 2.0 Int32.Parse can be replaced with Int32.TryParse, but this don't work in .NetCF.

I suggest to replace the Int32.Parse with:

int retval = 0;
try
{
  retval = Int32.Parse(stream.ReadString(length),
  CultureInfo.InvariantCulture);
}
catch 
{}

and use the retval result.

Here is the new code:

IMySqlValue IMySqlValue.ReadValue(MySqlStream stream, long length, bool nullVal)
{
	if (nullVal)
		return new MySqlInt32((this as IMySqlValue).MySqlDbType, true);
	if (length == -1)
		return new MySqlInt32((this as IMySqlValue).MySqlDbType,
			 stream.ReadInteger(4));
	else
	{
		int retval = 0;
		try
		{
			retval = Int32.Parse(stream.ReadString(length),
			 CultureInfo.InvariantCulture);
		}
		catch 
		{}
		return new MySqlInt32((this as IMySqlValue).MySqlDbType, retval);
	}
}

Thanks.
______________________________________
Angelo Abatemarco
Ecolnet Sistemi
angelo(dot)abatemarco(at)vipnet(dot)it
[7 Nov 2008 19:07] Reggie Burnett
Null values should be handled by this code:

 if (nullVal)
      return new MySqlInt32((this as IMySqlValue).MySqlDbType, true);
[12 Nov 2008 11:26] Angelo Abatemarco
Hi Reggie Burnett, 
you're right, because my supposition was wrong, since the exception happens when the fields are not null. Perhaps some not "parseable" characters are present in the stream, like an empty (but not null) string.
[13 Feb 2009 14:45] Tonci Grgin
Angelo, you can always use wireshark or something to confirm your suspicion. In the meantime, can I get a repeatable test case attached?
[14 Mar 2009 0:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[26 Oct 2011 17:47] Reggie Burnett
We were not given enough info to identify the particular problem