Description:
I'm getting a "System.InvalidCastException:
Specified cast is not valid." when I have a BIGINT UNSIGNED field and try to
convert it to UInt64.
Here is the table structure:
create table user
(
UserId BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
ScreenName VARCHAR(50) NOT NULL,
AccessKey VARCHAR(50) NOT NULL,
LastModified DATETIME NOT NULL
);
The count line is throwing the exception:
System.InvalidCastException: Specified cast is not valid.
at TagML.DbLayer.DbClient.CountSimilarUsers(String screenName) in c:\inetpub\wwwroot\tagmlws\dblayer\connection.cs:line 76
at TagML.WebServices.ClientManager.AnnounceUser(String screenName) in c:\inetpub\wwwroot\tagmlws\clientmanager.asmx.cs:line 78
How to repeat:
static public UInt64 CountSimilarUsers(String screenName)
{
IDbConnection myConnection = DBFactory.CreateConnection();
myConnection.Open();
IDbCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "SELECT COUNT(*) FROM tagml_user WHERE ScreenName like
@SN";
IDbDataParameter sn = myCommand.CreateParameter();
sn.ParameterName = "@sn";
sn.Value = screenName + "%";
myCommand.Parameters.Add(sn);
UInt64 count = (UInt64)myCommand.ExecuteScalar();
myConnection.Close();
return count;
}
Suggested fix:
The bug isn't specific to counts. The error occurs in returned fields as well.
A fix in the client code can do this:
UInt64 count = (UInt64)(Int32)myCommand.ExecuteScalar();
However, it would be best if this type could be supported. UInt32 holds the largest int that the current system will allow.
Description: I'm getting a "System.InvalidCastException: Specified cast is not valid." when I have a BIGINT UNSIGNED field and try to convert it to UInt64. Here is the table structure: create table user ( UserId BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL, ScreenName VARCHAR(50) NOT NULL, AccessKey VARCHAR(50) NOT NULL, LastModified DATETIME NOT NULL ); The count line is throwing the exception: System.InvalidCastException: Specified cast is not valid. at TagML.DbLayer.DbClient.CountSimilarUsers(String screenName) in c:\inetpub\wwwroot\tagmlws\dblayer\connection.cs:line 76 at TagML.WebServices.ClientManager.AnnounceUser(String screenName) in c:\inetpub\wwwroot\tagmlws\clientmanager.asmx.cs:line 78 How to repeat: static public UInt64 CountSimilarUsers(String screenName) { IDbConnection myConnection = DBFactory.CreateConnection(); myConnection.Open(); IDbCommand myCommand = myConnection.CreateCommand(); myCommand.CommandText = "SELECT COUNT(*) FROM tagml_user WHERE ScreenName like @SN"; IDbDataParameter sn = myCommand.CreateParameter(); sn.ParameterName = "@sn"; sn.Value = screenName + "%"; myCommand.Parameters.Add(sn); UInt64 count = (UInt64)myCommand.ExecuteScalar(); myConnection.Close(); return count; } Suggested fix: The bug isn't specific to counts. The error occurs in returned fields as well. A fix in the client code can do this: UInt64 count = (UInt64)(Int32)myCommand.ExecuteScalar(); However, it would be best if this type could be supported. UInt32 holds the largest int that the current system will allow.