views:

72

answers:

1

Hi guys,

I am having a very weird issue in the way strings get stored in my database, and as a result, I am getting these "unterminated string literal" errors in Javascript.

Here's an overview of what I am doing:

Platform: C#/ASP.NET MVC 1.0, SQL Server 2005, SparkViewEngine, YUI 2

In my view, I serialize an object into a JSON data structure using Json.NET from NewtonSoft.

<script type="text/javascript">
    // <![CDATA[ 
    var data = YAHOO.lang.JSON.parse("${Newtonsoft.Json.JsonConvert.Serialize(Model)}");
    ....
</script>

Normally, this works, but I noticed that one of the fields I pull from the database contains the following data, which causes the string to not be formed properly.

The database field is an NVARCHAR(2000).

For some of the entries, I get these weird characters in the string when I copy and paste from the database to notepad.

Compile ?Analysis ?& ?Recommendations? deck

In Firebug, it shows as a bunch of line breaks:

Analysis & Recommendations deck","StartDate":"1/19/10","FinishDate":"1/26/10","Duration":6.0,"

Compile 
Analysis 
& 
Recommendations
 deck

UPDATE After talking to the user, I discovered that they were using the copying and pasting from a word document onto the HTML form.

The form itself uses the YUI Connection Manager to make an asynchronous POST call (AJAX) to save the form values. The database saved the form field value along with any encoding associated with it.

It seems like there are characters in Word that are printable, but not in ASCII. Is there any way to detect this and encode correctly?

+2  A: 

This appears to be a case of you not handling the quote characters properly. You have several ways to go. The way I use for comments for our web site is to make sure all quotes are encoded, both single and double, so that both your SQL, ASPX & Javascript won't hiccup. Our method is for customer comment fields so we're overboard on the conversions.

Function SafeComment(ByVal strInput)
' Renders Any Comment Codes Harmless And Leaves Them HTML readable In An eMail Or Web Page
' Try: SafeComment("`~!@#$%^&*()_+=-{}][|\'"";:<>?/.,")
    SafeComment = ""
    If Len(strInput) = 0 Then Exit Function
    SafeComment =   Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                        Server.HtmlEncode(Trim(strInput)), _
                    "-", "&#45;"), ":", "&#58;"), "|", "&#124;"), _
                    "`", "&#96;"), "(", "&#40;"), ")", "&#41;"), _
                    "%", "&#37;"), "^", "&#94;"), """", "&#34;"), _
                    "/", "&#47;"), "*", "&#42;"), "'", "&#39;")
End Function

It's not pretty, but it does the job.

Dave
Thanks. I think the issue was the user copy and pasted a characters that were NOT printable ASCII characters.
AbeP
For that case we took the unicode characters as Ascii and then mapped the resulting 8-Bit characters to appropriate 7-bit printable ascii characters.
Dave
To be more precise, the user copied text from a word document onto the form. I'm not sure what characters got pasted, but they caused the string to not get terminated correctly; is the solution you mentioned work for such cases? Thanks again!
AbeP
For that type of thing we used a function that mapped all of the characters out of the x20 to x7e range into the x20 to x7e range. That one is even worse. I created an array of chararacter values. The code does an ASC() of the character and goes to that location in the array to create output character. We had to do that because MS-Dynamics data mapping will not accept any characters outside of that range. Any characters that aren't mapped are ignored.
Dave