Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2007

Tip: Looking for answers? Try searching our database.

Display literal string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lucas - 12 Dec 2007 16:37 GMT
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(SqlException exception,
Boolean breakConnection)\r\n   at
System.Data.SqlClient.SqlInternalConnection.OnError(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(SqlException
exception, Boolean breakConnection)
  at System.Data.SqlClient.SqlInternalConnection.OnError(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!
Alberto Poblacion - 12 Dec 2007 16:58 GMT
> I want to display an exception in a javascript alert and I'm rendering
> that client code using
[quoted text clipped - 5 lines]
> 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();
   }
Lucas - 12 Dec 2007 18:37 GMT
It's a good solution, thanks!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.