473,809 Members | 2,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Display literal string

Hi,

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

ClientScript.Re gisterStartupSc ript(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.Sq lClient.SqlExce ption: Cannot insert the value NULL into
column 'id_excepcion', table 'CI_Core.dbo.Tb l_GDC_Excepcion '; column
does not allow nulls. INSERT fails.\r\n at
System.Data.Sql Client.SqlConne ction.OnError(S qlException exception,
Boolean breakConnection )\r\n at
System.Data.Sql Client.SqlInter nalConnection.O nError(SqlExcep tion
exception, Boolean breakConnection )\r\n at
System.Data.Sql Client......"

appears in html as:

"System.Data.Sq lClient.SqlExce ption: Cannot insert the value NULL into
column 'id_excepcion', table 'CI_Core.dbo.Tb l_GDC_Excepcion '; column
does not allow nulls. INSERT fails.
at System.Data.Sql Client.SqlConne ction.OnError(S qlException
exception, Boolean breakConnection )
at System.Data.Sql Client.SqlInter nalConnection.O nError(SqlExcep tion
exception, Boolean breakConnection )
at System.Data.Sql Client......."

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 3748
"Lucas" <la********@gma il.comwrote in message
news:b5******** *************** ***********@l32 g2000hse.google groups.com...
I want to display an exception in a javascript alert and I'm rendering
that client code using

ClientScript.Re gisterStartupSc ript(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(stri ng text)
{
System.Text.Str ingBuilder sb = new System.Text.Str ingBuilder();
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
17526
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 * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
7
4368
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
4
5401
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 literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
2
12025
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 however that will just capture a Full Name from Text Box and display information from the Active Directory into a DataTable. I will display that code below.
5
2765
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 and department name NAME (link DEPTNM
0
1959
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 of string manipulation where they seek through the string back and forth doing replacements to substitute in the needed HTML code. I am convinced that this can be done with a few regular expressions. Unfortunately my knowledge of regular...
10
11331
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
2900
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 memory holding the string and the string might very well be held in read only memory. However, I am sure that I read somewhere that the declaration char a = "string literal", even though a is an array (and I understand the differences between...
4
2862
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* pointing to the string literal is eliminated during evaluation, so the fact it is unknowable doesn't matter. ('A' is assumed not be to mapped to NUL.) #if "A" #else #error "A" is false
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.