John John - 5 months ago 33
SQL Question

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);


Answer

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.