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 

Tag Cloud

AJAX (4) ASP (6) ASP.Net (47) Error Reporting (4) Web Service (1) WSDL (1) Atlas (2) Business (74) Business Start-up Advice (25) Client (13) Expanding Your Business (17) C# (15) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CSS (3) dasBlog (4) DDD (1) Design (9) Icons (1) Development (11) General (39) Christmas (6) Fun and Games (11) Internet (22) Random (46) RX-8 (8) Home Cinema (2) Hosting (1) IIS (8) iPhone (1) JavaScript (2) Marketing (5) Email (1) Multipack (1) Networking (2) Nintendo (1) OS Commerce (1) Photography (1) PHP (1) PowerShell (1) Press Release (1) Productivity (2) Security (2) SEO (5) Server Maintenance (3) Server Management (8) Social Networking (1) Experiment (1) Software (9) Office (5) Visual Studio (11) Windows (4) Vista (1) SQL (1) SQL Server (13) Stored Procedure (1) Testing (1) The Site Doctor (102) Turnover Challenge (1) Twitter (1) Umbraco (15) 2009 (1) Web Development (50) WebDD (33) Wii (1)

Blog Archive

Search

<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
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!

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.2.8279.16125

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

© 2010 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"

    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
    Name
    E-mail
    (will show your gravatar icon)
    Home page

    Comment (HTML not allowed)  

    Live Comment Preview