1

I'm writing once again about my encoding issue... Now with some code samples.

In a nutshell: when saving to database input data, some language specyfic characters like polish 'ń' won't save - insted 'n' is saved. On the other hand, string: Adams æbler, with æ is saving.

Here is code begind code that does save stuff and displays data:

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "";
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["encoding"].ConnectionString))
        {
            conn.Open();
            var command = conn.CreateCommand();
            command.CommandText = "SELECT * FROM users";
            var reader = command.ExecuteReader();
            while (reader.Read())
            {
                Label1.Text += reader.GetString(0);
            }                
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["encoding"].ConnectionString))
        {
            conn.Open();
            var command = conn.CreateCommand();
            command.CommandText = "INSERT INTO users VALUES('" + Surname.Text + "')";
            command.ExecuteNonQuery();
        }  
    }

Default.aspx has meta tag:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Table looks like this:

CREATE TABLE [dbo].[Users]([Surname] [nvarchar](50) COLLATE Latin1_General_CI_AI NULL ) ON [PRIMARY]

I don't know what else is needed to solve the issue. Help appreciated.

Thanks, Paweł

5
  • Ah, when I issued: SELECT Surname, CAST(Surname AS VARBINARY(30)) AS Expr1 FROM Users, bytes for alledged 'ń' character are 0x6E00 - so in database 'n' character is stored. Commented Sep 15, 2010 at 20:45
  • Does Surnme.Text contain the correct value if you debug in Button1_Click? What value appears to be sent to SQL Server if you use SQL Profiler? Commented Sep 15, 2010 at 21:02
  • Yes. Surname.Text has correct value. Also checking it with Encoding.Unicode.GetBytes returns "good" bytes. I haven't checked it with profiler yet. Commented Sep 15, 2010 at 21:19
  • And profiled checked: INSERT INTO users VALUES('ń') was sent... Commented Sep 15, 2010 at 21:25
  • I thought this question looked familiar. I just saw your other question again. Commented Sep 15, 2010 at 22:43

2 Answers 2

1

Your table definition appears to be the problem. Surname is defined with COLLATE Latin1_General_CI_AI. The AI part indicates Accent Insensitive. You would need to change the collation to Latin1_General_CI_AS, to preserve the data.

Sign up to request clarification or add additional context in comments.

Comments

0

finally I figured out what is wrong. I missed N prefix before string (it marks string as UNICODE):

command.CommandText = "INSERT INTO users VALUES(N'" + Surname.Text + "')";

Thanks, Paweł

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.