Ieuan Walker
Ieuan Walker

Reputation: 35

C# System.FormatException: Input string was not in a correct format

im trying to create a string using String.Format and add parameters. But for some reason, I'm receiving the error -

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

Here is my code

string queryPattern =
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
"PREFIX db: <http://dbpedia.org/ontology/> " +
"PREFIX prop: < http://dbpedia.org/property/> " +
"SELECT ?movieLink ?title ?genreLink ?genre ?releaseDate " +
"WHERE { " +
    "?movieLink rdf:type db:Film; " +
                "foaf:name ?title. " +
    "OPTIONAL { ?movieLink prop:genre ?genreLink. " +
                "?genreLink rdfs:label ?genre. " +
                "FILTER(lang(?genre) = 'en') }. " +
    "OPTIONAL{ ?movieLink <http://dbpedia.org/ontology/releaseDate> ?releaseDate }. " +

    "{0}" +
    "{1}" +
    "FILTER(lang(?title) = 'en') " +
"}" +
"ORDER BY DESC(?releaseDate)" +
"{2}";

return String.Format(queryPattern, genreMatch, dateMatch, limit);

Any help would be greatly appreciated.

Upvotes: 2

Views: 2116

Answers (3)

MayowaO
MayowaO

Reputation: 370

Not sure how you are passing values to the parameters. You can use the parameters directly in the string instead of using the string format function.

string queryPattern =
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
"PREFIX db: <http://dbpedia.org/ontology/> " +
"PREFIX prop: < http://dbpedia.org/property/> " +
"SELECT ?movieLink ?title ?genreLink ?genre ?releaseDate " +
"WHERE { " +
"?movieLink rdf:type db:Film; " +
            "foaf:name ?title. " +
"OPTIONAL { ?movieLink prop:genre ?genreLink. " +
            "?genreLink rdfs:label ?genre. " +
            "FILTER(lang(?genre) = 'en') }. " +
"OPTIONAL{ ?movieLink <http://dbpedia.org/ontology/releaseDate> ?releaseDate              }. " +

genreMatch +
dateMatch +
"FILTER(lang(?title) = 'en') " +
"}" +
"ORDER BY DESC(?releaseDate)" +
limit;

return queryPattern;

Upvotes: 0

Heinz Kessler
Heinz Kessler

Reputation: 1670

You are not allowed to use curly braces in your format string other than for placeholders, thus "{0}" is OK, "{some text}" is not. You can solve your problem by using double curly braces:"{{some text}}"

Upvotes: 2

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43896

string.Format uses curly braces ({}) to indicate placeholders. Your format string is invalid because it contains another few curly braces.

You need to escape those braces by doubling them:

string s = "Teststring {{ {0} }}";
string r = string.Format(s, 42);

results in r:

Teststring { 42 }

So for example your line

"WHERE { " +

should be

"WHERE {{ " +

Upvotes: 4

Related Questions