locked
How to transform DropDownList Value to UTF8mb4 string? RRS feed

  • Question

  • User102575017 posted

    Hello and best regards,

    I have Hebrew letters in the ListItem Text, and I want to insert it into the table in the database that has the Unicode utf8mb4.

    When I get the text of the DropDownList ListItem and insert it into the table, it inserts questions marks (????? ??) not Hebrew characters.

    When I get the text of a TextBox that has Hebrew letters and inserts it into the table, it inserts questions marks too (??? ??).

    How to solve this problem?

    Thank you,

    Majid Abu Rmelah.

    Friday, July 12, 2019 10:27 PM

Answers

  • User102575017 posted

    Evening everyone,

    I solved my problem and that is by adding "Charset=utf8;" to my connection string in Web.config.

    So my ConnectionString looks like this:

    <connectionStrings>
         <add name="LocalMysql" connectionString="Data Source=Localhost; port=3306; Integrated Security=False; Initial Catalog=mymarbadya; User Id=root; Password=; Charset=utf8;" providerName="Mysql.Data.MysqlClient"/>
         <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=ASP-2-1-alpha1;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>

    Thank you guys for participating to help, I really appreciate that! ^^

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 18, 2019 7:22 PM

All replies

  • User839733648 posted

    Hi Majid Abu Rmelah,

    Majid Abu Rmelah

    When I get the text of the DropDownList ListItem and insert it into the table, it inserts questions marks (????? ??) not Hebrew characters.

    When I get the text of a TextBox that has Hebrew letters and inserts it into the table, it inserts questions marks too (??? ??).

    If you are using SQL Server Database, I suggest that you could do the following to insert the Hebrew letters successfully.

    You should first make sure that the field you are trying to put the characters into is an nvarchar (not varchar). 

    Then you could write the sql statement like:

                cmd.Parameters.Add(new SqlParameter("@Item", SqlDbType.NVarChar, 50));
    

    Here is a simple demo and hope it will be helpful to you.

    .aspx

           <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>עִבְרִית</asp:ListItem>
                <asp:ListItem>ברוך הבא</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click"/>

    code-behind.

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(constr); con.Open(); string sql = "insert into TestLetter(Item) values (@Item)"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.Add(new SqlParameter("@Item", SqlDbType.NVarChar, 50)); cmd.Parameters["@Item"].Value = DropDownList1.SelectedValue; cmd.ExecuteNonQuery(); con.Close(); }

    result:

    For more, you could refer to: https://stackoverflow.com/questions/6268718/how-do-you-encode-hebrew-characters-to-a-database-without-getting 

    Best Regards,

    Jenifer

    Monday, July 15, 2019 4:08 AM
  • User753101303 posted

    Hi,

    Knowing which db you are using could help. You checked it really happens only at the db level ? Make sure you are using nvarchar/nchar as well as the proper encoding. In some db you may have perhaps to tell which character set is used so that the db driver can encode/decode as needed...

    Tuesday, July 16, 2019 11:09 AM
  • User102575017 posted

    Evening everyone,

    I solved my problem and that is by adding "Charset=utf8;" to my connection string in Web.config.

    So my ConnectionString looks like this:

    <connectionStrings>
         <add name="LocalMysql" connectionString="Data Source=Localhost; port=3306; Integrated Security=False; Initial Catalog=mymarbadya; User Id=root; Password=; Charset=utf8;" providerName="Mysql.Data.MysqlClient"/>
         <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=ASP-2-1-alpha1;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>

    Thank you guys for participating to help, I really appreciate that! ^^

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 18, 2019 7:22 PM