Bug #6227 GetBoolean returns wrong values
Submitted: 23 Oct 2004 13:43 Modified: 24 Oct 2004 1:37
Reporter: Puiu Hrenciuc Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:1.0.0 CVS as on 23.Oct.2004 OS:Microsoft Windows (Windows XP SP2)
Assigned to: Reggie Burnett CPU Architecture:Any

[23 Oct 2004 13:43] Puiu Hrenciuc
Description:
I have downloaded the CVS on 23.10.2004 and I have built the binary. Many
bugs are fixed, but a new one appeared : all boolean values aren't corectly
read. All GetBoolean returns true even if the value in database is false (0).
Taking a look at the Value member in the MySQL Connector the value is always
(SByte) 48 !!! 

How to repeat:
Read any boolean field from a table. GetBoolean always returns true and the Value is (SByte)48
[23 Oct 2004 18:47] Puiu Hrenciuc
I found out that the Value is actually the ASCII code of the field value ('0' ASCII 48 for false and '1' ASCII 49 for true ) so for the moment the solution was to replace the GetBoolean function to :

public bool GetBoolean(int i)
{
	return (sbyte)GetValue(i)==49?true:false;
}

but it is a temporary solution until you fix it into official sources.
[23 Oct 2004 20:04] Puiu Hrenciuc
I have found out that the real problem is in byte handling, even GetByte fails. It throws "Specified cast is not valid." on :

MySqlByte b = (MySqlByte)currentResult[i];

within the GetByte function. Taking a look at the values I saw that it contained  49('0) instead of 0. So I digged into source code and saw that the real problem is in MySqlUByte.SetData and MySqlByte.SetData methods. I have replaced the code like this :

		internal override void SetData(PacketReader reader, long length)
		{
			//mValue = (sbyte)reader.ReadByte();
			if (length == -1) 
			{
				mValue = (SByte)reader.ReadByte();
			}
			else 
			{
				string value = reader.ReadString( length );
				mValue = SByte.Parse( value );
			}
			
		}
for MySqlByte and :

		internal override void SetData(PacketReader reader, long length)
		{
			//mValue = (byte)reader.ReadByte();
			if (length == -1) 
			{
				mValue = (byte)reader.ReadByte();
			}
			else 
			{
				string value = reader.ReadString( length );
				mValue = Byte.Parse( value );
			}
		}
for MySqlUByte.

Also there was a casting problem in datareader.GetByte method so I've modified it like this :

		public byte GetByte(int i)
		{
			object tValue=currentResult[i];
			if (tValue.GetType()==typeof(MySqlUByte))
			{
				return ((MySqlUByte)tValue).Value;
			} 
			else
			{
				return (byte)((MySqlByte)tValue).Value;
			}
		}

and leaved the GetBoolean to its original state :

		public bool GetBoolean(int i)
		{
			return Convert.ToBoolean(GetValue(i));
		}

and everything is working fine. I'll keep testing the Connector.NET and provide as much bugreports as I can reveal.
[24 Oct 2004 1:37] 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