John
John

Reputation: 51

ASP.net: Question Marks instead of Arabic text

I am using Visual studio 2015 Asp.net C#, and MySql workbench.

I've two issues:

  1. In inserting Arabic data into the database (MYSQL workbench), it appears question marks ????? I found solution asked me to change the type of data from varchar to nvarchar then add capital N before Arabic string (INSERT INTO TableName (ColumnName) values(N’ArabicText’)) How can I apply that in this code? I tried to add N in different place but it did not work?
  2. When I determine the type of data as NVARCHAR(45) in MySQL workbench, the program appear message :change applied successfully, however when I open the table I found varchar(45) returned again.

     string query = "UPDATE learningobjects SET level=?level, subjectName=?    subjectName WHERE ID=?ID";
        using (MySqlCommand cmd = new MySqlCommand(query, cn))
        {
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("?level", Level);
            cmd.Parameters.AddWithValue("?ID", lblid.Text);
    
            cmd.Parameters.AddWithValue("?subjectName", txtsubject.Text);
    

Upvotes: 3

Views: 3535

Answers (2)

Rick James
Rick James

Reputation: 142540

When trying to use utf8/utf8mb4, if you see Question Marks (regular ones, not black diamonds),

  • The bytes to be stored are not encoded as utf8. Fix this.
  • The column in the database is CHARACTER SET utf8 (or utf8mb4). Fix this.
  • Also, check that the connection during reading is utf8.

Upvotes: 0

Berin Loritsch
Berin Loritsch

Reputation: 11473

MySQL is not SQL Server, so the solutions are a little different but the cause of the question marks is the same. What you are seeing is a character conversion error which can happen in any point in your request/response chain.

Microsoft SQL Server introduced NCHAR, NVARCHAR, and NTEXT to handle unicode. These are all Microsoft specific keywords so they won't work with MySQL. These days, MySQL should be set up to handle unicode character sets by default, but just in case you may want to check out these links:

And to be safe, make sure your connection string specifies the character set (should be OK if the rest of MySQL is set up for UTF)

Lastly, something else that should not be overlooked, is your forms need to be UTF compliant as well. (ref)

<form action="demo_form" accept-charset="utf-8">
</form>

Which can also be specified like this with ASP.NET MVC:

@using (Html.BeginForm(null, null, FormMethod.Post, new { accept_charset = "utf-8" })) {
  <!-- content of form -->
}

I would start with how data gets in to your database, and then see what happens with the data after you put it in.

Upvotes: 2

Related Questions