12

I'm using an EDM model in my project.

When I insert russian words in the database via a post request I get ??????

Controller:

[Authorize]
[HttpPost]
public string DescEdit(FormCollection formValues)
{
    var CurrentUserPhoto = User.Identity.Name;
    string x = Request.Form["id"];
    Int64 id = Convert.ToInt64(x);
    photos upPhotoDesc = photosRepository.GetPhotosById(id, CurrentUserPhoto);
    upPhotoDesc.description = Request.Form["value"];
    photosRepository.Save();

    return Request.Form["value"];
}
  1. In the database all charset are set to utf-8
  2. In the layout page content enc type is utf-8

Database code:

CREATE TABLE `photos` (
  `id` bigint(255) NOT NULL AUTO_INCREMENT,
  `done` tinyint(1) NOT NULL DEFAULT '0',
  `imgsmall` varchar(255) NOT NULL DEFAULT '',
  `imgcrop` varchar(255) NOT NULL DEFAULT '',
  `imgmiddle` varchar(255) NOT NULL DEFAULT '',
  `imgbig` varchar(255) NOT NULL DEFAULT '',
  `full_size` varchar(255) NOT NULL DEFAULT '',
  `description` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `permission` tinyint(1) NOT NULL DEFAULT '0',
  `userid` int(11) NOT NULL DEFAULT '0',
  `userlogin` varchar(255) NOT NULL DEFAULT '',
  `rating` smallint(5) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `indx_photos_1` (`id`,`userlogin`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
default
  • 11,485
  • 9
  • 66
  • 102
Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45
  • 1
    Well, *something* in the chain of events must be doing a lossy conversion to/from an obsolete character set. You will need to identify that something and make sure that it uses Unicode. – Timwi May 04 '11 at 20:32
  • Shot in the dark: Are you sure the database actually contains question marks? Maybe only the thing you’re using to *look at* the database displays question marks. – Timwi May 04 '11 at 20:32
  • i'm trying to understant where is this "something" for 2 hours already :)) – Evgeniy Labunskiy May 04 '11 at 20:33
  • 1
    Have you stepped through the code with the debugger and made sure that `Request.Form["value"]` still contains the correct Cyrillic? – Timwi May 04 '11 at 20:36
  • @Timwi - Yes, im sure. PhpMyADmin show and MySQL-Front – Evgeniy Labunskiy May 04 '11 at 20:37
  • @Timwi - return Request.Form["value"] return to me the correct russian string, so I think that the form value is ok – Evgeniy Labunskiy May 04 '11 at 20:37
  • the post data sent in urlencode-like string.... may be this is the problem?.. – Evgeniy Labunskiy May 04 '11 at 20:40
  • 1
    If you've not seen it already I wrote this article recently on common characterset problems in PHP/LAMP http://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php/ – James C May 04 '11 at 21:22
  • thanks for articul! But still not working... :((( – Evgeniy Labunskiy May 04 '11 at 21:32
  • please check my answer at http://stackoverflow.com/questions/12632240/entityframework-update-or-insert-chinese-or-non-english-text/24765790#24765790 – Saboor Awan Jul 15 '14 at 18:44
  • please check my answer at http://stackoverflow.com/questions/12632240/entityframework-update-or-insert-chinese-or-non-english-text/24765790#24765790 – Saboor Awan Jul 15 '14 at 18:47

1 Answers1

24

Add charset=utf8 to the connection string of Entity Framework
Here is a working add node:

<add name="photostorageEntities"
     connectionString="metadata=res://*/Models.Photos.csdl|res://*/Models.Photos.ssdl|res://*/Models.Photos.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=ServerIP;User Id=UID;password=PASS;Persist Security Info=True;database=photostorage; Charset=utf8&quot;"
     providerName="System.Data.EntityClient" />
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45