Search code examples
c#postgresqlnpgsql

C# with Npgsql - Can't write CLR type System.String with handler type Int32Handler


i have problem when im trying to add integer to Postgresql database. Varchars works fine, but when i use code:

var parameter = cmd.CreateParameter();
parameter.Value = Int32.Parse(x.Value.ToString());
Console.WriteLine(parameter.Value);
parameter.ParameterName = x.Key.ToString();
cmd.Parameters.Add(new NpgsqlParameter("confirmations",NpgsqlTypes.NpgsqlDbType.Integer));
for (int search=0; search != cmd.Parameters.Count; search++)

error in executing, where im using:

 cmd.ExecuteNonQuery();

sounds like:

Database problem: System.InvalidCastException: Can't write CLR type System.String with handler type Int32Handler\

at lambda_method(Closure , NpgsqlTypeHandler , Object , NpgsqlLengthCache& , NpgsqlParameter )

at Npgsql.NpgsqlParameter.ValidateAndGetLength() in C:\projects\npgsql\src\Npgsql\NpgsqlParameter.cs:line 553

at Npgsql.NpgsqlCommand.ValidateParameters() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 793

at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1141

at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1042

at Npgsql.NpgsqlCommand.ExecuteNonQuery() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1025

at BitcoinType.DatabaseManager.MakeInsert(Dictionary`2 requestData) in /Users/kamilkostrzewski/Projects/BitcoinType/BitcoinType/Program.cs:line 261

This did not helped me.


Solution

  • I had this problem and I solved like this. In my database I have a column that type Integer. If you want to add data this column, You should use NpgsqlDbType.Integer datatype. But when I use NpgsqlDbType.String datatype, I get that error.

    So you should change your NpgsqlDBtType. I hope it helps you.