Answered by:
Saving Hebrew text

Question
-
User-2012457684 posted
If I save Hebrew text in our legacy software (asp 3.0 aka classic asp) it somehow automatically converts it to html entity. When I try to do the same in the new software that I am writing using ASP.NET MVC it does not do this. instead it returns a bunch of question marks.
Entering this
var string1="לצפייה בדף זה בעברית, לחצו כאן"
in classic asp saves to the database encoded as the html entities
var string1="לצפייה בדף זה בעברית, לחצו כאן"
and doing the same save via the mvc page saves as this in the database
var string1="?????- ?????? ??? ?? ??????, ???? ???"
both are saved using the exact same stored procedure.
Classic ASP
Set cmd = Server.CreateObject("ADODB.Command") Set cmd.ActiveConnection = oConn cmd.CommandText = "AddScript" cmd.CommandType = adCmdStoredProc With cmd .Parameters.Append .CreateParameter("@SectionID",adInteger,adParamInput,4, CInt(SectionID)) .Parameters.Append .CreateParameter("@LanguageID",adInteger,adParamInput,4, CInt(LanguageID)) .Parameters.Append .CreateParameter("@Script",adLongVarChar,adParamInput,Len(Script),Script) End With cmd.Execute Set cmd = Nothing
MVC Code
//in class public void AddScript(int LanguageID, int SectionID, string Script) { using (SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString)) { using (SqlCommand oCmd = new SqlCommand()) { oCmd.CommandText = "AddScript"; oCmd.CommandType = CommandType.StoredProcedure; oCmd.Parameters.AddWithValue("@SectionID", SectionID); oCmd.Parameters.AddWithValue("@LanguageID", LanguageID); oCmd.Parameters.AddWithValue("@Script",Script); oCmd.Connection = oConn; oConn.Open(); oCmd.ExecuteNonQuery(); oConn.Close(); oCmd.Dispose(); } } } //inside controller [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult Edit(int LanguageID, int SectionID, string Script) { Assessment test = new Assessment(); test.AddScript(LanguageID, SectionID,Script); return RedirectToAction("Index"); }
I am planning on replacing the legacy code but need to be able to edit this if the need arises.
Tuesday, October 8, 2019 8:44 PM
Answers
-
User-2012457684 posted
Thanks, I was not aware that using the AddWithValue method did not save as nvarchar.
So I switched to using the Add method where I specify the SqlDbType.NVarChar and it saved properly.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 9, 2019 4:58 PM
All replies
-
User475983607 posted
Your post is a little confusing. Your classic ASP code is encoding the characters. Why can't you do the same in you MVC application?
[HttpGet] public ActionResult Index() { var string1 = "לצפייה בדף זה בעברית, לחצו כאן"; string encoded = Server.HtmlEncode(string1); return Content(encoded); }
Tuesday, October 8, 2019 9:23 PM -
User-2012457684 posted
Actually No it isn't it just uses the following
Dim LanguageID Dim SectionID Dim Script LanguageID = Request.Form("LanguageID") SectionID = Request.Form("SectionID") Script = Request.Form("Script")
And in this case I cannot encode all of it because it is creating some text in the language mixed with Javascript code that is then used to fill a script which is displayed to the user at the beginning of each section.
Here is how I display on the actual page that the user sees.
Set cmd = Server.CreateObject("ADODB.Command") Set cmd.ActiveConnection = oConn cmd.CommandType = adCmdStoredProc cmd.CommandText = "GetScript" cmd.Parameters.Refresh cmd.Parameters(1) = DisplaySection cmd.Parameters(2) = LanguageID Set oRs = cmd.Execute Script = oRs("Script") Set oRs = Nothing %> <script language="JavaScript" type="text/javascript"> <% Response.Write ReplaceScriptVariables(Script) %> </script>
The ReplaceScriptVariables function replaces some variables that are in the scripts depending on assessment, section etc.
fill the script like thisTuesday, October 8, 2019 11:12 PM -
User-17257777 posted
Hi mj1223,
According to your description, I am still confused about your current MVC codes now.
If you want to encode the string then insert it into the databsae, you could refer to mgebhard solution.
Besides, if you want to store the Hebrew text into database directly, I suggest you could check your database table’s column type to make sure it’s NVARCHAR not VARCHAR.
Details ,you could refer to below test result.
Use VARCHAR:
Use NVARCHAR:
Best Regards,
Jiadong Meng
Wednesday, October 9, 2019 9:48 AM -
User-2012457684 posted
Yes, it is nvarchar, that goes without saying. mgebhard's solution works when I am not saving a mixture of English and Hebrew. Which the JavaScript code will be.
When it is all Hebrew I can use that solution. I cannot do so however, when it isn't and that is the dilemma I am faced with.
Wednesday, October 9, 2019 2:23 PM -
User475983607 posted
mj1223
Yes, it is nvarchar, that goes without saying. mgebhard's solution works when I am not saving a mixture of English and Hebrew. Which the JavaScript code will be.
When it is all Hebrew I can use that solution. I cannot do so however, when it isn't and that is the dilemma I am faced with.
The input parameter is defined using AddWithValue(). Specify NVARCHAR.
oCmd.Parameters.AddWithValue("@Script",Script);
Also, your original code input parameter is VARCHAR not NVARCHAR and you're code is HTML encoding the values.
Keep in mind, the community cannot provide an accurate solution without accurate sample code.
Wednesday, October 9, 2019 2:53 PM -
User-2012457684 posted
Thanks, I was not aware that using the AddWithValue method did not save as nvarchar.
So I switched to using the Add method where I specify the SqlDbType.NVarChar and it saved properly.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 9, 2019 4:58 PM