unrecognized escape sequence

  • Thread starter Thread starter Abraham Luna
  • Start date Start date
A

Abraham Luna

can anyone tell me why this string throws an error:

strValues += "<tr><td><a href=\"\#\" onclick=\"javascript:" + strTextBox +
".value='" + drRDK[strValue].ToString().Replace("\\", "\\\\").Replace("'",
"\\'").Replace('"', '""') + "';" + strDiv + ".style.visibility='hidden';" +
strDiv + ".innerHTML='';\">" + strOutput + "</a></td></tr>\n\r";

my code edit colors it correctly but it throws an error
please help
 
Abraham Luna said:
can anyone tell me why this string throws an error:

strValues += "<tr><td><a href=\"\#\" onclick=\"javascript:" + strTextBox +
".value='" + drRDK[strValue].ToString().Replace("\\", "\\\\").Replace("'",
"\\'").Replace('"', '""') + "';" + strDiv + ".style.visibility='hidden';" +
strDiv + ".innerHTML='';\">" + strOutput + "</a></td></tr>\n\r";

my code edit colors it correctly but it throws an error
please help

# doesn't need escaping - where you've got \#, that's an illegal escape
code.

Note that your code would be *much* easiest to read if you'd break each
bit onto a different line, even if you still have it within the same
statement. For example:

strValues += "<tr><td><a href=\"\#\" onclick=\"javascript:" +
strTextBox +
".value='" +
drRDK[strValue].ToString()
.Replace("\\", "\\\\")
.Replace("'", "\\'")
.Replace('"', '""') +
"';" +
strDiv +
".style.visibility='hidden';" +
strDiv +
".innerHTML='';\">" +
strOutput +
"</a></td></tr>\n\r";

Using a StringBuilder and breaking it up further would almost certainly
add more to the readability too - *and* make it easier to find out
where the error is when you get one.
 
thank you for the answer. this is what i came up with and it finally works:

string varValue = drRDK[strValue].ToString().Replace("\\",
"\\\\").Replace("'", "\\'").Replace(@"""", @"""""");
strValues += @"<tr><td><a href=""#"" onclick=""javascript:" + strTextBox
+ ".value='" + varValue + "';" + strDiv + ".style.visibility='hidden';" +
strDiv + @".innerHTML='';"">" + strOutput + "</a></td></tr>" + "\n\r";

do u know anything about html? cause that code above works :), but the
javascript it produces sometimes doesn't. this is something the above
generates that doesn't work:

<tr><td><a href="#"
onclick="javascript:ctl00_ctl00_cphMainContent_cphCustomers_tbName.value='DOUBLE
""H"" ENTERPRISES
INC';divOfNames.style.visibility='hidden';divOfNames.innerHTML='';">14585 -
DOUBLE "H" ENTERPRISES INC</a></td></tr>

how can i get the ""H"" to work right. thank you


Jon Skeet said:
Abraham Luna said:
can anyone tell me why this string throws an error:

strValues += "<tr><td><a href=\"\#\" onclick=\"javascript:" + strTextBox
+
".value='" + drRDK[strValue].ToString().Replace("\\",
"\\\\").Replace("'",
"\\'").Replace('"', '""') + "';" + strDiv + ".style.visibility='hidden';"
+
strDiv + ".innerHTML='';\">" + strOutput + "</a></td></tr>\n\r";

my code edit colors it correctly but it throws an error
please help

# doesn't need escaping - where you've got \#, that's an illegal escape
code.

Note that your code would be *much* easiest to read if you'd break each
bit onto a different line, even if you still have it within the same
statement. For example:

strValues += "<tr><td><a href=\"\#\" onclick=\"javascript:" +
strTextBox +
".value='" +
drRDK[strValue].ToString()
.Replace("\\", "\\\\")
.Replace("'", "\\'")
.Replace('"', '""') +
"';" +
strDiv +
".style.visibility='hidden';" +
strDiv +
".innerHTML='';\">" +
strOutput +
"</a></td></tr>\n\r";

Using a StringBuilder and breaking it up further would almost certainly
add more to the readability too - *and* make it easier to find out
where the error is when you get one.
 
strValues += "<tr><td><a href=\"#\" onclick=\"javascript:" + strTextBox +
".value='" + drRDK[strValue].ToString().Replace("\\", "\\\\").Replace("'",
"\\'").Replace("\"", "\"\"") + "';" + strDiv + ".style.visibility='hidden';"
+ strDiv + ".innerHTML='';\">" + strOutput + "</a></td></tr>\n\r";

Escape sequence fixed. I would suggest using StringBuilder for this.
 
Abraham Luna said:
thank you for the answer. this is what i came up with and it finally works:

string varValue = drRDK[strValue].ToString().Replace("\\",
"\\\\").Replace("'", "\\'").Replace(@"""", @"""""");
strValues += @"<tr><td><a href=""#"" onclick=""javascript:" + strTextBox
+ ".value='" + varValue + "';" + strDiv + ".style.visibility='hidden';" +
strDiv + @".innerHTML='';"">" + strOutput + "</a></td></tr>" + "\n\r";

It may work, but I certainly wouldn't want to be the one to debug it.
Why have it as one nasty big block when you can split it up nicely?
do u know anything about html? cause that code above works :), but the
javascript it produces sometimes doesn't. this is something the above
generates that doesn't work:

<tr><td><a href="#"
onclick="javascript:ctl00_ctl00_cphMainContent_cphCustomers_tbName.value='DOUBLE
""H"" ENTERPRISES
INC';divOfNames.style.visibility='hidden';divOfNames.innerHTML='';">14585 -
DOUBLE "H" ENTERPRISES INC</a></td></tr>

how can i get the ""H"" to work right. thank you

I suspect you want \"H\" in the JavaScript - but the right way to test
this is to edit it in a text file. Get it right, and *then* make your
C# code generate it.
 
Back
Top