String.Format Question

  • Thread starter Thread starter Akeel
  • Start date Start date
A

Akeel

Hi I have the following code

char[] splitter={','};
string[] fields=row["DataLine"].ToString().Split(splitter)
string serviceDefintion=""

// row["DataLine"].ToString().Split(splitter
// conatins the followin
//"MigrationRef,ContactType,FirstName,LastName,MiddleName,Title

string _storedProcedure="create spProc @MigrationRef={0}, @ContactType={2}"
serviceDefintion=String.Format(_storedProcedure,fields)

// I would expect the value of _storedProcedure="create spProc @MigrationRef=MigrationRef, @ContactType=ContactType

I get an error when it tries this format :

ystem.SystemException: {"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."

If I do the following it works fine

serviceDefintion=String.Format(_storedProcedure,fields[0],fields[1],fields[2])

The problem that I have is that the fields will vary in length from 2 items in the array to 55 items. I could create a case statement or I could do this in a loop but the string.format function seems perfect and I wonder if there is a differnt way I need to pass my array to it - can anyone help

Thanks in advance

Akeel
 
Akeel said:
I get an error when it tries this format :

ystem.SystemException: {"Index (zero based) must be greater than or equal
to zero and less than the size of the argument list."}
If I do the following it works fine.

serviceDefintion=String.Format(_storedProcedure,fields[0],fields[1],fields[2
]);

The problem that I have is that the fields will vary in length from 2 items
in the array to 55 items.
I could create a case statement or I could do this in a loop
but the string.format function seems perfect and I wonder
if there is a differnt way I need to pass my array to it - can anyone
help?

Unluckly the string.Format() function require a param array, so if you pass
an array it is considered just as the first item.

You can resolve using a loop on the fields array and a
_storeProcedure.Replace("{" + i + "}", fields)
 
Thanks for your help.

Gutted - it was a smart solution. I guess its time to start looping!!

I will look in to this further to see if there is a way around this but I think your right.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top