Nick k.
Nick k.

Reputation: 3

Storing Japanese characters from TextBox to SQL table

Storing Japanese characters from a form TextBox to SQL table appears as question marks. I'm just trying to make a table that holds the Japanese text and the English translation to make my life easier as I'm studying Japanese. Searching for a solution 2 days now nothing seems to be working.

I am not even sure if this is actually a good practice for storing text to data table.

Also column where I want the Japanese character stored is set to nvarchar(50).

private void addWordButton_Click(object sender, EventArgs e)
{

  con.Open();
  System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();

  cmd.CommandType = System.Data.CommandType.Text;
  cmd.CommandText = "INSERT Words (WordJapanese, WordEnglish) VALUES ('" + newJPwordTxt.Text + "', '" + 
  newENwordTxt.Text + "')";
  cmd.Connection = con;

  cmd.ExecuteNonQuery();
  con.Close();

}


Upvotes: 0

Views: 532

Answers (2)

Salah Akbari
Salah Akbari

Reputation: 39956

It seems you have missed the into keyword in your Insert statement, as a second note, you need to be aware that this kind of string concatenation is avoided and it is open to SQL Injection attack. You should always use parameterized queries to avoid SQL Injection:

cmd.CommandText = "INSERT into Words (WordJapanese, WordEnglish) VALUES (@WordJapanese, @WordEnglish)";

cmd.Parameters.Add("@WordJapanese", SqlDbType.NVarChar, 50).Value = newJPwordTxt.Text;
cmd.Parameters.Add("@WordEnglish", SqlDbType.NVarChar, 50).Value = newENwordTxt.Text;

Upvotes: 2

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Your query has syntax issues and secondly you should be using parameterized queries to safeguard from SQL Injection.

The following should be good :

cmd.CommandText = "INSERT INTO Words(WordJapanese, WordEnglish) VALUES (@Japanse, @English)";

cmd.Parameters.Add("@Japanse", SqlDbType.NVarChar, 50).Value = newJPwordTxt.Text;
cmd.Parameters.Add("@English", SqlDbType.NVarChar, 50).Value = newENwordTxt.Text;

Upvotes: 2

Related Questions