Question

The following code keep giving me error saying Input string was not in a correct format, but I am pretty sure it is right, isn't it?

int id = 112;

String[] newData = { "1", "2", "21", "reidb", "reidb", "reidb", "reidb", "aa", 
                      "Some description", "11", "2012-02-28", "2012-01-29", "true", "1", "1", 
                      "true", "note note note", "true", "1", "2011-12-03", "45"};

String data = "{ cmd: \"save magellan deal\", data: { id: {0} , AId: {1}, " +
            "CId: {2}, CCId:{3}, LA: \"{4}\", BA: \"{5}\" , " +
            "LSA: \"{6}\" , BSA: \"{7}\" , \"path: \"{8}\"," +
            "dscp: \"{9}\", SI: \"{10}\", CD: \"{11}\", " +
            "period: \"{12}\", IsStatic: {13}, LSD: {14}, LC: {15}, RB: {16},} " +
            "Notes: \"{17}\", IsEE: {18}, RBy: {19}, DPDD: \"{20}\", LId: {21} } }";


String cmd = String.Format(data, id.toString(), newData);

Anyone any ideas?

=== EDIT ===

after fixing the braces, a new error of "Index (zero based) must be greater than or equal to zero and less than the size of the argument list." is given. the newData has 21 and plus the id.toString(), should be exact 22?

Was it helpful?

Solution

Escape the "{", "}" (by duplicating them) in the format string:

"{{ cmd: \"save magellan deal\", data: {{ id: {0} , AId: {1}, " +
"CId: {2}, CCId:{3}, LA: \"{4}\", BA: \"{5}\" , " +
"LSA: \"{6}\" , BSA: \"{7}\" , \"path: \"{8}\"," +
"dscp: \"{9}\", SI: \"{10}\", CD: \"{11}\", " +
"period: \"{12}\", IsStatic: {13}, LSD: {14}, LC: {15}, RB: {16},}} " +
"Notes: \"{17}\", IsEE: {18}, RBy: {19}, DPDD: \"{20}\", LId: {21} }} }}"

And you have 22 elements in the format string and 21 in the array.

OTHER TIPS

You have 21 elements in newData but use 22 placeholders (numbered 0 to 21).

Also, you must escape literal '{' in your input data

Opening and closing braces are interpreted as starting and ending a format item. Consequently, you must use an escape sequence to display a literal opening brace or closing brace. Specify two opening braces ("{{") in the fixed text to display one opening brace ("{"), or two closing braces ("}}") to display one closing brace ("}"). Braces in a format item are interpreted sequentially in the order they are encountered. Interpreting nested braces is not supported.

http://msdn.microsoft.com/en-us/library/txafckwd.aspx

I've said as much in the comments, but I think it's worth an answer by now:

Don't use string.Format (in this case). It's slower, and less readable. Just use plain string concatenation with meaningful variable names if you need complex concatenations - these will be faster and more maintainable. Faster because for a fixed number of segments plain + is the fastest option due to compiler optimizations, and more maintainable because the reader can see the context within the string in which the variable is put.

As Hans Passant noted, it looks like you're making a javascript literal. If that's the case, consider a Json serializer like Json.NET instead:

JsonConvert.SerializeObject(new {
    cmd = "save magellan deal",
    data = new {
        id = 12345, AId = 1, CId = 2, CCId = 21,
        LA = "reidb", BA = "reidb", LSA = "reidb", BSA = "reidb",
        path = "aa", dscp = "Some description", SI = 11,
        CD = "2012-02-28", period = "2012-01-29",
        IsStatic = true, LSD = 1, LC = 1, RB = true,
    },
    Notes = "note note note",
    IsEE = true, RBy = 1, DPDD = "2011-12-03", LId = 45
})

While Json.NET does support DateTime serialization, there's not standardized format for dates in JS, so these are serialized as strings in an iso 8601 format. This might not be what you need, hence the date strings in this example - I'd try the iso format first, though, as that's the simplest solution.

In addition to escaping your curly braces also try changing your String[] definition to:

int id = 112;

String[] newData = {id.ToString(), "1", "2", "21", "reidb", "reidb", "reidb", "reidb", "aa", 
          "Some description", "11", "2012-02-28", "2012-01-29", "true", "1", "1", 
          "true", "note note note", "true", "1", "2011-12-03", "45"};

and change your String.Format to:

String cmd = String.Format(data,newData);

What

String cmd = String.Format(data, id.toString(), newData);  

does is: trying to format the String data , using just 2 strings..not 22!!
Which ones?...1)id.ToString() and 2)newData.ToString()
Thats obviously wrong

How you can see that this is the problem...? Leave just {0} and {1} in data string and print it.It compiles ok and see what you get.
Of course you have to use {{ instead of { where you want a string literal

Fully working Solution:

int id = 112;

String[] newData = { id.ToString(),"1", "2", "21", "reidb", "reidb", "reidb", "reidb", "aa", 
                  "Some description", "11", "2012-02-28", "2012-01-29", "true", "1", "1", 
                  "true", "note note note", "true", "1", "2011-12-03", "45"};

String data = "{{ cmd: \"save magellan deal\", data: {{ id: {0} , AId: {1}, " +
                    "CId: {2}, CCId:{3}, LA: \"{4}\", BA: \"{5}\" , " +
                    "LSA: \"{6}\" , BSA: \"{7}\" , \"path: \"{8}\"," +
                    "dscp: \"{9}\", SI: \"{10}\", CD: \"{11}\", " +
                    "period: \"{12}\", IsStatic: {13}, LSD: {14}, LC: {15}, RB: {16},}} " +
                    "Notes: \"{17}\", IsEE: {18}, RBy: {19}, DPDD: \"{20}\", LId: {21} }} }}";


String cmd = String.Format(data, newData);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top