Question

I get this error when i try to write to my postgresql DB.

NpgsqlException: ERROR: 22021: invalid byte sequence for encoding "UTF8": 0xe5 0x27 0x2c

It only happen when i use danish letters (æ ø å).

Is there a way to set it to use ex. Unicode ?

I use Npgsql in a C# project.

I have no problem at all when iam not useing the danish letters.

Was it helpful?

Solution 3

NpgsqlConnection conn = new NpgsqlConnection(
         "Server="+ConnectionInfo +";"+
         "Port=5432;" +
         "User Id=MyAdmin;" +
         "Password=Test;" +
         "Database=MyDB;"+
         "Encoding = Unicode"
         );

     conn.Open();

OTHER TIPS

Your app is buggy. It's sending latin-1 encoded strings as if they were utf-8.

The byte sequence in the error is the latin-1 for the string:

å',

per:

regress=> SELECT convert_from(BYTEA '\xe5272c', 'latin-1');
 convert_from 
--------------
 å',
(1 row)

You must either set client_encoding correctly to reflect the encoding of the text you're sending, or (preferably) use proper C# unicode strings when working with nPgSQL.

I had same problem when I do copy and paste from word document or notepad into my C# application textbox field. The simple solution is to add "Encoding = Unicode" into your postgresql connection string of your c# application. How simple could that be. I was relieved after going through three days of google search.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top