473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Display literal string

Hi,

I want to display an exception in a javascript alert and I'm rendering
that client code using

ClientScript.RegisterStartupScript(GetType(), key,
string.Format(@"<script>alert('ERROR:\r\n\r\n{0}') ;</script>",
ex.ToString()));

The problem is that ex.ToString() contains escape characters that not
appear literal in html.

For example:
"System.Data.SqlClient.SqlException: Cannot insert the value NULL into
column 'id_excepcion', table 'CI_Core.dbo.Tbl_GDC_Excepcion'; column
does not allow nulls. INSERT fails.\r\n at
System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception,
Boolean breakConnection)\r\n at
System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)\r\n at
System.Data.SqlClient......"

appears in html as:

"System.Data.SqlClient.SqlException: Cannot insert the value NULL into
column 'id_excepcion', table 'CI_Core.dbo.Tbl_GDC_Excepcion'; column
does not allow nulls. INSERT fails.
at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption
exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient......."

causing an error in the javascript alert because the line breaks.

How can i put the ex.ToString() in html as literal?

Thanks!
Dec 12 '07 #1
2 3732
"Lucas" <la********@gmail.comwrote in message
news:b5**********************************@l32g2000 hse.googlegroups.com...
I want to display an exception in a javascript alert and I'm rendering
that client code using

ClientScript.RegisterStartupScript(GetType(), key,
string.Format(@"<script>alert('ERROR:\r\n\r\n{0}') ;</script>",
ex.ToString()));

The problem is that ex.ToString() contains escape characters that not
appear literal in html.
I pass the text of the exception through the following function, which I
don't claim to be optimal but gets the job done:

public static string EscapeText(string text)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (char c in text)
{
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();
}
Dec 12 '07 #2
It's a good solution, thanks!
Dec 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
2
by: j.m.osterman | last post by:
I haven't found exactly what I've been trying to do. All I am trying to do for now is just display usernames from Active Directory into a ListBox control on a page. I have found some code...
5
by: PHPBABY3 | last post by:
Hi, 1. I have two SQL tables. I will call them employees and departments: EMP: LAST_NAME, FIRST_NAME, DEPTNM DEPT: NUM, NAME Input: text string FIND Output: the LAST_NAME, FIRST_NAME...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
4
by: zaimoni | last post by:
I've already calculated that the following are valid and should not error, as both just end up with the character literal 'A' being their control expression. The unspecified value of the char*...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.