1

I use ASP.NET and use MySqlConnection to run an insert query that has an emoji inside the text:

Hi 

Connection string is :

string connectionString = "SERVER=" + Server + ";" + "DATABASE=" + Database + ";" + "UID=" + Uid + ";" + "PASSWORD=" + Password + ";charset=utf8mb4;";

my query is :

query = @"insert into outgoing_message(from_mobile_number, dest_mobile_number, message_body, creation_date, due_date, recID, DCS) values ('" + From + "', '" + item.Mobile + "', N'" + "Hi " + "', now(), now(),'" + item.RecID + "','" + Dcs + "');SELECT LAST_INSERT_ID();";

I get the following error:

Incorrect string value: '\xF0\x9F\x98\x80y' for column `...`.`...`.`message_body` at row 1

But I did all the configurations correctly.

The attached image is also illustrative

  • Post the query and actual code. What you posted is escape sequences, not UTF8. You don't need to do **anything** to use UTF8 with ASP.NET, and this question is the proof. SO is an ASP.NET site storing text in `nvarchar` columns in SQL Server, whose pages use UTF8 encoding just like any other web site. .NET and Windows strings are Unicode, specifically UTF16. That's why you were able to post that emoji without encoding your own text or contacting SO support for special treatment. – Panagiotis Kanavos Sep 28 '20 at 07:51
  • The query output you show, especially `character_set_client`, applies to that console connection. Perhaps the MySQL driver connects with another character set; try appending `;charset=utf8mb4` to your connection string. – CodeCaster Sep 28 '20 at 07:54
  • It's possible that the MySQL version or the OS it runs on is too old to handle newer emojis - although that particular emoji isn't that new. I've run in similar problems with Ruby on old Debian versions that didn't support emojis. Or it could be the code itself that mangles the query, trying to encode what doesn't need encoding – Panagiotis Kanavos Sep 28 '20 at 07:57

0 Answers0