Tim

Footprints in the snow of a warped mind

String.Format and Input string was not in a correct format Error

Where to find me

Flickr Icon  Twitter Icon  Linked In Icon  FaceBook Icon  Windows Live Alerts Butterfly  RSS 2.0 

Business Protection by Crisis Cover

Tag Cloud

AJAX (4) Analysis (1) ASP (6) ASP.Net (56) Error Reporting (4) Web Service (2) WSDL (1) Atlas (2) Born In The Barn (1) Business (85) Business Start-up Advice (28) Client (16) Expanding Your Business (20) Recruitment (1) C# (20) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CMS (1) Code Management (1) Cohorts (1) Commerce4Umbraco (1) Content (1) Content Management (1) Content Management System (1) CSS (3) dasBlog (5) DDD (1) Design (10) Icons (1) Development (21) eCommerce (8) Employment (2) General (39) Christmas (6) Fun and Games (11) Internet (22) Random (46) RX-8 (8) Helpful Script (3) Home Cinema (2) Hosting (2) HTML (1) IIS (11) iPhone (1) JavaScript (4) jQuery (1) Marketing (6) Email (1) Multipack (1) MVC (1) Networking (3) Nintendo (1) Nuget (1) OS Commerce (1) Payment (1) Photography (1) PHP (1) PowerShell (2) Press Release (1) Productivity (2) Random Thought (1) Security (2) SEO (5) Server Maintenance (6) Server Management (11) Social Media (2) Social Networking (3) Experiment (1) Software (10) Office (5) Visual Studio (13) Windows (4) Vista (1) SQL (8) SQL Server (19) Statistics (1) Stored Procedure (1) TeaCommerce (1) Testing (2) The Site Doctor (124) Turnover Challenge (1) Twitter (3) uCommerce (9) Umbraco (29) 2009 (1) 2011 (1) Web Development (65) WebDD (33) Wii (1) XSLT (1)

Blog Archive

Search

<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

Recent Comments

Blog Archive

Various Links

Blogs I Read

[Feed] Google Blog
Official Google Webmaster Central Blog
[Feed] Matt Cutts
Gadgets, Google, and SEO
[Feed] 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!
[Feed] Sam's Blog
Sam is one of my younger brothers studying Product Design and Manufacture at Loughborough, this is his blog :) Enjoy!

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

newtelligence dasBlog 2.2.8279.16125

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

© 2012 Tim Gaunt.

Sign In

# Monday, October 02, 2006

String.Format and Input string was not in a correct format Error

Monday, October 02, 2006 6:26:29 PM (GMT Daylight Time, UTC+01:00)

I ran into (another) interesting error today when using String.Format1. I was getting the error message "Input string was not in a correct format.". In this instance I was calling the content from an XML file, the data was wrapped in CDATA tags so there shouldn't have been an issue with line breaks etc. On investigating the error further I found it was being caused because the content was a HTML page. More specifically, the String.Format error "Input string was not in a correct format." was being caused by the CSS declaration's curly braces.

To work around the method perceiving them as placeholders, simply replace each curly brace "{" with double braces "{{".

To replicate the error:

String.Format("<style type=\"text/css\">p{font-size: 1.2em;}</style><p>Your username is: {0}</p>", "UserName");

The fix in place:

String.Format("<style type=\"text/css\">p{{font-size: 1.2em;}}</style><p>Your username is: {0}</p>", "UserName");

1The String.Format method accepts a format string which can include place holders (designated by the curly braces {} and a number that refers to the location of the item that should appear in the list). A quick example of String.Format:

String.Format("This is some text the date is {0}.", DateTime.Today.ToShortDateString);

Would produce: "This is some text the date is 02/10/2006"

 

Don't forget to follow me on Twitter.

Monday, November 17, 2008 12:51:46 PM (GMT Standard Time, UTC+00:00)
Yes, you are absolutely 100% right. Thanks.
Pritam Pratihar
Wednesday, May 20, 2009 5:04:18 PM (GMT Daylight Time, UTC+01:00)
Genius! Had the same problem, thanks for the fix.
George
Monday, August 17, 2009 5:13:47 PM (GMT Daylight Time, UTC+01:00)
Nice one! Was exactly the problem.
Craig
Thursday, November 05, 2009 2:07:18 PM (GMT Standard Time, UTC+00:00)
Bingo!. Thanks.
Adick
Wednesday, November 18, 2009 3:58:07 PM (GMT Standard Time, UTC+00:00)
Sweet!!
Monday, December 28, 2009 4:49:55 PM (GMT Standard Time, UTC+00:00)
Thanks! Looks like this post is a gift that keeps on giving.
Tim
Monday, July 19, 2010 3:49:44 PM (GMT Daylight Time, UTC+01:00)
great just spent 3 hours very puzzled on this!
thanks. sean
sean rogers
Wednesday, August 04, 2010 8:32:55 PM (GMT Daylight Time, UTC+01:00)
Thanx!!! I too spent some time trying to understand this...
Nim
Wednesday, December 22, 2010 3:55:31 AM (GMT Standard Time, UTC+00:00)
Thanks! Helped me get pass JSON issue.
Nick
Wednesday, January 12, 2011 8:33:01 AM (GMT Standard Time, UTC+00:00)
Hi


Tnx , I Face To That Problem Some Times

Regards
alikolahdoozan
Wednesday, February 16, 2011 1:07:50 PM (GMT Standard Time, UTC+00:00)
You saved my day, thank you.
Jean-Yves
Tuesday, March 01, 2011 3:55:26 PM (GMT Standard Time, UTC+00:00)
I had this issue with Crystal Reports where clauses! Thanks for the tip!
Thursday, March 03, 2011 7:06:57 PM (GMT Standard Time, UTC+00:00)
Crystal Reports? Really? wow didn't know they'd be affected too, thanks for the heads up Michael
Sunday, July 10, 2011 10:04:45 PM (GMT Daylight Time, UTC+01:00)
Fantastic...Thanks.
Rob
Monday, January 02, 2012 10:49:17 AM (GMT Standard Time, UTC+00:00)
Thanks, that's was a lifesaver :)
Antonio
Tuesday, January 03, 2012 6:58:47 PM (GMT Standard Time, UTC+00:00)
Saved my hide as well. Thanks doc.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Live Comment Preview