2

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.

3 Answers 3

4

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 {{ " + 
Sign up to request clarification or add additional context in comments.

Comments

2

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}}"

Comments

0

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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.