Sandokan
Sandokan

Reputation: 1105

How to insert an index in string.format?

I'm trying to add an index to string.format string, for example:

 For i = 1 To Dataset.Tables(0).Columns.Count - 1
       query_builder.Append(String.Format("@parameter{i}", i))
 Next 

What I'm trying to achieve is get in a similar result:

@parameter1
@parameter2
@parameter3 etc....

But I get this error:

Input string format not correct

why?

Upvotes: 0

Views: 107

Answers (2)

Ric
Ric

Reputation: 13248

query_builder.Append(String.Format("@parameter{i}", i))

Should be

query_builder.Append(String.Format("@parameter{0}", i))

or

query_builder.AppendFormat("@parameter{0}", i)

Upvotes: 3

Meta-Knight
Meta-Knight

Reputation: 17875

You must specify a numeric value between brackets:

For i = 1 To Dataset.Tables(0).Columns.Count - 1
   query_builder.Append(String.Format("@parameter{0}", i))
Next 

{0} corresponds to the item at index 0 (first item) in the list of parameters to String.Format, which is variable i in your case.

Upvotes: 1

Related Questions