user61307
user61307

Reputation: 325

How do I properly encode foreign accented characters in ASP.NET MVC/SQL application (C-R-U examples please)

What's the best practice for reading/saving/updating accented characters via HTML/MVC/LINQ-to-SQL?

We have a web application where users submit articles from all over the world. Some users, particularly those in Europe, submit articles with accented/extended characters. We are using Linq to SQL, C#, SQL 2005. All our db tables are normalized and Linq to SQL is used as the data mapping layer.

The bug we want to fix is that in our MVC3 / SQL 2005 application, accented characters in words submitted by users via the normal

using (Html.BeginForm()) {... }

block to an [HttpPost] attributed controller are rendered as '?' on the Save event. For example, all works well until foreign characters are entered, in which case we might have a record that talks about "Proven?al activities" in the db.

What is the best practice for encoding these in an MVC3, SQL2005-ish way?

I would really appreciate it if someone outlined the create, read and update operations for something like this, or tell me what magic thing I'm missing. Thanks for any advice!

Upvotes: 2

Views: 980

Answers (1)

gbn
gbn

Reputation: 432271

You need to use

  • nvarchar types (not varchar)
  • literals with the N prefix eg WHERE x = N'foo'

Example: only N prefix + nvarchar saves OK

DECLARE @test TABLE (Broken varchar(10), Working nvarchar(10))
INSERT @test VALUES (N'Value 化化', N'Value 化化')
INSERT @test VALUES ('Value ける', 'Value ける')

SELECT * FROM @test

There is no global magic setting either...

Edit:

I'd also run SQL profiler to see if you are sending characters
I now suspect you're breaking things before it hits the database.

Taking Western European (DE+FR+Nordics) it works OK without my suggestions above

DECLARE @test TABLE (NowWorking varchar(10), Working nvarchar(10))
INSERT @test VALUES (N'öéäàüè', N'öéäàüè')
INSERT @test VALUES ('öéäàüè', 'öéäàüè')

SELECT * FROM @test

Upvotes: 3

Related Questions