Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I received the following error from a string.Format(...) operation:

System.FormatException - Input string was not in a correct format.

I have a resource dictionary which contains an entry with a basic html page.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title of the document</title>

    <style>
        * { 
            margin: 0; 
            padding: 0; 
        }

        body { 
            font-family: 'Arial, Verdana', Fallback, sans-serif; 
            font-size: 14px;
        }

        table { 
            background-color: #eeeeee; 
            width: 100%; 
            padding: 10px;
            margin: 5px;
        }

        h1 {
            font-size: 16px;
            font-weight: bold;
            margin: 20px 0 10px 0;
        }
    </style>
</head>

<body>
    text<br />

    <table>
        <tr>
            <td>Text 1</td>
            <td>{0}</td>
        </tr>
        <tr>
            <td>Text 2</td>
            <td>{1}</td>
        </tr>
        <tr>
            <td>Text 3</td>
            <td>{2}</td>
        </tr>
        <tr>
            <td>Text 4</td>
            <td>{3}</td>
        </tr>
        <tr>
            <td>Text 5</td>
            <td>{4}</td>
        </tr>
        <tr>
            <td>Text 6</td>
            <td>{5}</td>
        </tr>
        <tr>
            <td>Text 7</td>
            <td>{6}</td>
        </tr>
    </table>
    <br />

    <h1>Header1</h1>

    <table>
        <tr>
            <td><a href="http://www.google.com">http://www.google.com</a></td>
        </tr>
        <tr>
            <td>Text 8: {7}</td>
        </tr>
    </table>
    <br />
</body>

The string.Format(...) operations looks like this:

var emailHtml = string.Format(
                    WebUtility.HtmlEncode(EmailResource.EmailTemplate),
                    "text1",
                    "text2",
                    "text3",
                    "text4",
                    "text5",
                    "text6",
                    "text7",
                    "text8");

Does anyone know what error I made? Isn't it possible to fill in the placeholders in the resource dictionary like this?

share|improve this question
    
I don't think you should be HtmlEncoding the entire template.. you should only encode the individual text1, text2, etc, values if they need to be encoded (in this case they all do) – Joe Philllips Aug 19 '15 at 14:03
5  
If i were a betting man, those unescaped { and } (in your CSS definition) are the cause. Braces in a format input must be escaped ({ => {{, }} => }}). – Brad Christie Aug 19 '15 at 14:03
up vote 7 down vote accepted

You have a lot of curly braces which are not format specifiers, for example:

    { 
        margin: 0; 
        padding: 0; 
    }

Either you need to duplicate them:

    {{
        margin: 0; 
        padding: 0; 
    }}

Or using different formatting approach (like template engine).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.