Tim

Footprints in the snow of a warped mind

Tag Cloud

AJAX (4) ASP (6) ASP.Net (40) Error Reporting (2) Web Service (1) WSDL (1) Atlas (2) Business (67) Business Start-up Advice (24) Client (12) Expanding Your Business (15) C# (11) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CSS (3) dasBlog (2) DDD (1) Design (9) Icons (1) Development (8) General (39) Christmas (6) Fun and Games (11) Internet (20) Random (44) RX-8 (8) Home Cinema (2) Hosting (1) IIS (8) iPhone (1) JavaScript (2) Marketing (3) Multipack (1) Networking (2) Nintendo (1) OS Commerce (1) Photography (1) PHP (1) PowerShell (1) Press Release (1) Security (1) SEO (5) Server Maintenance (3) Server Management (8) Software (9) Office (5) Visual Studio (8) Windows (4) Vista (1) SQL Server (12) Testing (1) The Site Doctor (94) Turnover Challenge (1) Umbraco (10) Web Development (42) WebDD (33) Wii (1)

Atom 1.0 RSS 2.0 CDF 

Search

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Recent Comments

Blog Archive

Various Links

Blogs I Read

 Google Blog
Official Google Webmaster Central Blog
 Matt Cutts
Gadgets, Google, and SEO
 Ol' Deano's Blog
My mate Dean's blog on my space, equally as random as mine but not off on as much of a tangent!
 Sam's Blog
Sam is one of my younger brothers studying Product Design and Manufacture at Loughborough, this is his blog :) Enjoy!

Recent Tracks

last.fm - The Social Music Revolution

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

newtelligence dasBlog 2.0.7226.0

Send mail to the author(s) Email Me (Tim Gaunt)

© 2009 Tim Gaunt.

Sign In

Get Windows Live Alerts

 Wednesday, August 22, 2007

String.Format- Exception of type System.Web.HttpUnhandledException was thrown.

Wednesday, August 22, 2007 8:43:12 PM (GMT Standard Time, UTC+00:00)

Type
System.FormatException
Message
Exception of type 'System.Web.HttpUnhandledException' was thrown.
StackTrace
at System.Text.StringBuilder.FormatError()
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
Error Line
0

Just got that message (or at most "Exception of type 'System.Web.HttpUnhandledException' was thrown")? Puzzled? I was the first time I got it, I've been meaning to post about it for quite some time now so seeing as I got it again today I took the hint.

The error is horrifingly obvious when you know about it, in short, you've no doubt got some code that looks like this:

String.Format("<html><head><style type=\"text/css\">body{color: #fff;}</style><body>...");

Can you spot it now? Notice your style declaration is using the curly brackets? Basically String.Format is interpreting that as a placeholder i.e. {0} and is throwing a wobbly.

The solution is simple too, just replace all opening/closing brackets with two i.e:

String.Format("<html><head><style type=\"text/css\">body{{color: #fff;}}</style><body>...");

I hope that helps someone out there :)

P.S. Watch out for methods that use String.Format as they may catch you out in the same way -i.e. Subject of System.Net.Mail.MailMessage

Tuesday, October 14, 2008 3:18:56 AM (GMT Standard Time, UTC+00:00)
Can't you also escape the HTML-formatting string by adding an "@" character at the start, and then converting all quote characters to double-quotes?

String.Format(@"<html><head><style type=""text/css"">body{color: #fff;}</style><body>");
Tuesday, October 14, 2008 8:52:57 AM (GMT Standard Time, UTC+00:00)
I've not tried doing it that way but I don't think so no because the curly braces still represent place holders for the replace (think {0}) which is what causes the issue in this case.

Will give it a go though.

Tim
Sunday, October 19, 2008 7:23:52 PM (GMT Standard Time, UTC+00:00)
They remember not to follow links again ,
Sunday, October 19, 2008 7:51:57 PM (GMT Standard Time, UTC+00:00)
Words can't express your significance. ,
Sunday, October 19, 2008 7:59:35 PM (GMT Standard Time, UTC+00:00)
They remember not to follow links again ,
Sunday, October 19, 2008 8:02:08 PM (GMT Standard Time, UTC+00:00)
can work together, so many people ,
Name
E-mail
Home page

Comment (HTML not allowed)  

Live Comment Preview