invalid byte sequence for encoding

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
colomboz
Posts: 1
Joined: Mon 15 Sep 2008 08:56

invalid byte sequence for encoding

Post by colomboz » Mon 15 Sep 2008 09:14

Hello,
i'm using PostresSQLDirect for .NET2 and I've some problems with encoding.

Following a code sample witch causes the exception (the exception thrown is always the same “invalid byte sequence for encoding "UTF8": 0xe0a020”) :

Code: Select all

using (CoreLab.PostgreSql.PgSqlConnection cnn = new CoreLab.PostgreSql.PgSqlConnection())
            {
cnn.ConnectionString = @"User Id=sim;Host=localhost;Database=sim;Character Set=UTF8;Persist Security Info=True;Schema=gsim;password=sim";
                cnn.Charset = "UTF8";
                cnn.Open();
                CoreLab.PostgreSql.PgSqlCommand cmd = new CoreLab.PostgreSql.PgSqlCommand();
                cmd.Connection = cnn;
                cmd.CommandText = "INSERT INTO gsim.area_produttiva (nome) VALUES (:nome)";
                CoreLab.PostgreSql.PgSqlParameter p = new CoreLab.PostgreSql.PgSqlParameter();
                p.ParameterName = "nome";
                p.Value = "provà"; // <-- The problem is about "à" char
                p.PgSqlType = CoreLab.PostgreSql.PgSqlType.VarChar; 

                cmd.Parameters.Add(p);
                cmd.ExecuteNonQuery();
                cnn.Close();
            }


Db creating script:

CREATE DATABASE sim
WITH OWNER = sim
ENCODING = 'UTF8';


Thanks.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Wed 17 Sep 2008 07:27

We recommend to set Unicode=true instead of Charset = "UTF8".

Code: Select all

using (CoreLab.PostgreSql.PgSqlConnection cnn = new CoreLab.PostgreSql.PgSqlConnection())
{
cnn.ConnectionString = @"User Id=sim;Host=localhost;Database=sim;Persist Security Info=True;Schema=gsim;password=sim";
cnn.Unicode = true;
cnn.Open();
CoreLab.PostgreSql.PgSqlCommand cmd = new CoreLab.PostgreSql.PgSqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "INSERT INTO public.area_produttiva (nome) VALUES (:nome)";
CoreLab.PostgreSql.PgSqlParameter p = new CoreLab.PostgreSql.PgSqlParameter();
p.ParameterName = "nome";
p.Value = "provàè°"; // <-- there is no problem with "àè°" chars
p.PgSqlType = CoreLab.PostgreSql.PgSqlType.VarChar;
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();
cnn.Close();
}

Post Reply