Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2004
    Posts
    8

    Wink Unanswered: JavaScript with SQL NVARCHAR Datatype

    Hello,

    I am new to this forum. I have a question and hope someone might be able to help me. I did search this forum but can not find any solution. So here is my problem.

    I have a column call "Notes" that defined in the SQL Server 2000 database table as a nvarchar[2000] datatype. The "Notes" column contains both English and Russian text. Here goes:

    Use VBscript to retrieve "Notes" data from SQL database
    <%
    dim notes_buffer

    <!-- retrieve from database and assign to notes_buffer -->

    notes_buffer = recordSet.GetString
    response.write(" Notes: " & notes_buffer)
    %>

    I got the data from the SQL database and displayed it to the WebPage without any problem.

    Then try to use JavaScript functions, so I can parse and work on the data

    <script type="text/JavaScript">

    var buffer = <%=notes_buffer%> ; // not working
    or
    var buffer = '<%=notes_buffer%>'; // not working

    document.write(" BUF = " + buffer);
    </script>

    I tested the above statement with a "char" and "int" datatype no problem, but it is not working with a "nvarchar" datatype data.
    Is anyone out there know the soultion? I'm really appreciated some help here thanks.

    Rob

  2. #2
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    try

    <script type="text/JavaScript">

    var buffer = <%=cstr(notes_buffer)%> ; // not working
    or
    var buffer = '<%=cstr(notes_buffer)%>'; // not working

    document.write(" BUF = " + buffer);
    </script>

    or perhaps even escaping it.... if you don't escape it then when your text has a ' in it you will start running into errors.

  3. #3
    Join Date
    Feb 2004
    Posts
    8
    Thanks for the reply... I did try it, but it did not work... BTW, what is 'cstr'? I did notice that when I view the source contents (without or with cstr ), the variable buffer did pick up all the (English and Russian) text. Don't know why it can't display to the WebPage, and why it did not execut the <script> section.

    Rob

    Originally posted by rokslide
    try

    <script type="text/JavaScript">

    var buffer = <%=cstr(notes_buffer)%> ; // not working
    or
    var buffer = '<%=cstr(notes_buffer)%>'; // not working

    document.write(" BUF = " + buffer);
    </script>

    or perhaps even escaping it.... if you don't escape it then when your text has a ' in it you will start running into errors.

  4. #4
    Join Date
    Dec 2003
    Posts
    454
    What is that "notes_buffer = recordSet.GetString" ?

  5. #5
    Join Date
    Feb 2004
    Posts
    8
    notes_buffer contains the retrieved data from database.

    Originally posted by gyuan
    What is that "notes_buffer = recordSet.GetString" ?

  6. #6
    Join Date
    Dec 2003
    Posts
    454
    In JavaScript, when you pass a string to the variable, you need "". Try this:

    var buffer = "<%=notes_buffer%>";

  7. #7
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    Hi gyuan,

    Acutally, that's not true you can use either " or '. Some browsers have problems with " though.

    The cstr converts the variable to a string. It theory this happens implicitly when you concatentate strings (you first example that works).

    It is probably happening implicitly in the second example as well be the cstr will make it explicit.

    What is the JavaScript that is actually generated?

    var buffer = ''; ??

    or is it something else? Can you post the complete file that doesn't work? Perhaps it isn't this line that is the problem...?

  8. #8
    Join Date
    Feb 2004
    Posts
    8
    Hello again,

    I finally figured out why today, cause there were some Line Feed (LF) and Carriage Return (CR) in the data itself (document contains about 500 lines of english and russian text). I used VBCRLF to replace LF+CR to a "&" character and it worked fine now. Somehow the JavaScript variables do not like the LF or CR in the "string". So, the case is closed now. Thanks for paying attention to my problems:P

    My next assignments would be trying to PRINT (not window.print) a nicely formmated webpage. I'll really appreciated the anyone out there knows some of the tricks, or ASP/JavaScript examples or poits out threads that discuss this subject.

    Rob




    Originally posted by rokslide
    Hi gyuan,

    Acutally, that's not true you can use either " or '. Some browsers have problems with " though.

    The cstr converts the variable to a string. It theory this happens implicitly when you concatentate strings (you first example that works).

    It is probably happening implicitly in the second example as well be the cstr will make it explicit.

    What is the JavaScript that is actually generated?

    var buffer = ''; ??

    or is it something else? Can you post the complete file that doesn't work? Perhaps it isn't this line that is the problem...?

  9. #9
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    heheh thought it might be something like that...

    So you are replace your carriage returns with /n (or is it \n, I can never remember).

  10. #10
    Join Date
    Dec 2003
    Posts
    454
    Originally posted by rokslide
    Acutally, that's not true you can use either " or '. Some browsers have problems with " though.
    I'm not sure if double quotaion marks work with all kind of the broswers, but it works in my application.

  11. #11
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    heheheh that is the important thing right?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •