mrappe

In a MS c# Web Application in VS2005 I am trying to pass a SQL Server error message to a

JavaScript so that I can create an alert box. The error message from SQL Server contains the

' character. This will not work .In the JavaScript it is a delimiter so I have to escape it

(in JS) with a \'. When I do a search and replace in the code I have to use \\' since I have

to escape the \ in the c# string. When i try this the c# string shows \\' and not \'. If I

use \\\' it shows \\' and if I use \' it shows '. I cannot get it to show \' in the passed

string. If I use "This is a \\'test\\' it will only work correctly if it is output to a

textbox and show as "This is a \'test\' but in the string pased to JS it shows as "This is a

\\'test\\'".

When I try to use :
string escapedquote = @"\'";
In the debugger if I put a watch on the string this is still the value of it.

"ERROR: The data was NOT saved. MSG: Violation of PRIMARY KEY constraint \\'PK_tbl_Profiles\\'. Cannot insert duplicate key in object \\'tbl_Profiles\\'.\r\nThe statement has been terminated."

so I still have the same problem when the JS is run.

Here is the String:

"ERROR: The data was NOT saved. MSG: Violation of PRIMARY KEY constraint 'PK_tbl_Profiles'.

Cannot insert duplicate key in object 'tbl_Profiles'.\r\nThe statement has been terminated."

Here is the code:

namespace ftrdata
{
public class ftrUtils
{
public System.Web.UI.ClientScriptManager cs;
public void DoMsg(Type cstype ,string csname,string message, ClientScriptManager cs)
{
string escapedquote = "\\'";
string newmessage = message.Replace("'", escapedquote);
string strScript;
strScript = "<script language=javaScript>alert('" + newmessage +

"')</script>";
if (!cs.IsClientScriptBlockRegistered(cstype, csname))
{
cs.RegisterClientScriptBlock(cstype, csname, strScript, false);
}

}
}
}



Re: Visual C# General How to place \' in a c# string so that \' shows up in the text.

H. Tony

following shows \' for me with no problem.

Console.WriteLine("\\\'");







Re: Visual C# General How to place \' in a c# string so that \' shows up in the text.

mrappe

It makes sense that it would but what does it so as the string value in a watch If I put the string that shows a value of \\' in a textbox it also shows up as \' but when I use it in the script as shown in the code then the JS code that is generated it shows up as \\'.





Re: Visual C# General How to place \' in a c# string so that \' shows up in the text.

H. Tony

not sure what you mean, do you see \\' in watch window during debug

also you can try javascript escape() function

mrappe wrote:

It makes sense that it would but what does it so as the string value in a watch If I put the string that shows a value of \\' in a textbox it also shows up as \' but when I use it in the script as shown in the code then the JS code that is generated it shows up as \\'.






Re: Visual C# General How to place \' in a c# string so that \' shows up in the text.

Alberto Poblacion

After getting horribly frustrated trying to match the appropriate escape characters in C# that would in turn produce the corresponding escape characters in javascript, I finally wrote a function to encode special characters in hex, in order to avoid all of the trouble with the backslashes and the different types of quotes. It's probably not optimal, but it's not called often enough to deserve the effort of optimizing it.

Code Block

public static string FormatClientScriptText(string theText)

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

foreach (char c in theText)

{

int asc = Convert.ToInt32(c);

if (asc >= 0x28 && asc <= 0x5b) sb.Append(c);

else if (asc >= 0x5d && asc <= 0x7e) sb.Append(c);

else

{

string hex = string.Format("{0:x}", asc);

if (hex.Length < 2) hex = "0" + hex;

sb.AppendFormat("\\x" + hex);

}

}

return sb.ToString();

}