String.Format() Giving "Input String Is Not in Correct Format"

string.Format() giving Input string is not in correct format

string.Format() considers each '{' or '}' to be part of a placeholder (like '{0}' you already use). You need to escape each literal occurrence by doubling it.

So in your case do:

 string tmp = @"
if (UseImageFiles) {{
...
}}";

String.Format: Input string was not in a correct format

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.

C# String.Format Input string not in a correct Format

{ and } have special meanings in a format string. If you want the string to contain them literally, you have to escape them by doubling them:

string body = string.Format(@"{{
""credentials"": {{
""name"": ""{0}"",
""password"": ""{1}"",
""site"": {{
""contentUrl"": ""{2}""
}}
}}
}}", Username, Password, siteName);

Alternatively, as @mjwills has suggested, you can create an anonymous object and serialize this to JSON:

var obj = new { name = Username, password = Password, site = new { contentUrl = siteName }};
string body = new JavaScriptSerializer().Serialize(obj);

Exception during StringFormat: Input string was not in a correct format

@ruzgarustu's answer helped me identify the root cause of the issue.

I solved the issue by the below code. so basically I have removed the enclosing brackets.

AppLogger.InfoFormat(message.Trim('{', '}'), args);

string.Format exception Input string was not in a correct format

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

C# Input string was not in a correct format?

You need to escape your curly brackets:

"Number : {0} \nFloors:{1} \nStreet \n{{ \nName : {2},\nLength : {3}\n}}"

If you need { or } in the resulting string, you need to escape them as {{ and }}.

Input string was not in a correct format. asp.net

ValidationAttribute.FormatErrorMessage() is throwing Input string was not in a correct format error.

This is because of your Errormessages in your Model. For example [Required(ErrorMessage = "لطفا {۰} را وارد کنید")]

The error is because of {}. It has special meaning in C#. So you have to use escape character for that.

Example: (ErrorMessage = "Your characters should be in: !#$%&()*+,-./:;<=>?@[]^_{{|}}~"

So for escaping {, you have to use double {.

String format Error input string was not in a correct format.

You have to escape curly braces by doubling them:

int numberStored = 9;
record.VALUE = string.Format("{{\"FIELDS\":[{{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}}]}}", numberStored.ToString(), 0);

Input string was not in a correct format

The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer.

It's extremely unlikely that the text boxes will contain a valid integer immediately when the form is created - which is where you're getting the integer values. It would make much more sense to update a and b in the button click events (in the same way that you are in the constructor). Also, check out the Int.TryParse method - it's much easier to use if the string might not actually contain an integer - it doesn't throw an exception so it's easier to recover from.



Related Topics



Leave a reply



Submit