Maged E William
Maged E William

Reputation: 446

Arabic turn to strange Question marks

After typing in Arabic in the form I press an insert button to insert the fields in the db. But characters turn to strange question marks like :"?????". That doesn't happen with numbers or English characters.

How to type Arabic and still have it in the db as Arabic, not as "?????"?

enter image description here

Here is some code

SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["StoreConnectionString"].ToString());

        public AddBook()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                string X;
                cn.Open();
                SqlCommand cm = new SqlCommand("SELECT COUNT(*) FROM StoreItem", cn);
                Int32 count = (Int32)cm.ExecuteScalar();
                X = count.ToString();
                cn.Close();

                SqlCommand cmd = new SqlCommand("insert into StoreItem (ID, Title, Writer, Price, Qun, Date) Values ('" + X + "','" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "', '" + DateTime.Now.ToString("d/M/yyyy") + "')", cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();

                textBox1.Text = null;
                textBox2.Text = null;
                textBox3.Text = null;
                textBox4.Text = null;

                this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

And

private void Rsh()
{
    SqlDataAdapter ADAP = new SqlDataAdapter("Select * FROM StoreItem", cn);
    DataSet DS = new DataSet();
    ADAP.Fill(DS, "Store");
    dataGridView1.DataSource = DS.Tables["Store"];
    dataGridView1.Columns[1].Width = 200;
    dataGridView1.Columns[0].HeaderCell.Value = "#";
    dataGridView1.Columns[1].HeaderCell.Value = "عنوان الكتاب";
    dataGridView1.Columns[2].HeaderCell.Value = "الكاتب";
    dataGridView1.Columns[3].HeaderCell.Value = "السعر";
    dataGridView1.Columns[4].HeaderCell.Value = "الكمية";
    dataGridView1.Columns[5].HeaderCell.Value = "تاريخ دخول";
}

Upvotes: 4

Views: 3941

Answers (2)

Shaikh Farooque
Shaikh Farooque

Reputation: 2640

surly Your datatype for those column is varchar.

You just need to change it to nvarchar and it will be get saved. Also dont forget to add a prefix N. Refer to following query.

if name is nvarchar

insert into Emp (name) values (N'test')

Upvotes: 11

Belogix
Belogix

Reputation: 8147

Looking at your screen shot it looks as though you already have some unicode data but as I rough top-down walk through. You should be using nvarchar to store your strings as it is unicode it will record Arabic or extended characters without issue.

You also need to check that your character encoding is setup (UTF8 is usually a good bet) and passed through all various places correctly. If you are using SqlConnection and SqlCommand then it would be best to pass the value as a parameter to ensure that everything is being handled correctly and avoid injection attacks. If you are using Entity Framework there should be no additional steps to my knowledge.

Upvotes: 2

Related Questions