Question

In my code I have received, by way of a url, a comma delimited list of id's e.g.

12,13,14,15,16 etc..

I have got these into a string (Tools) which I have Split.

I now need to loop through each value and use it in an insert statement but I have got stuck can anyone help.

The C# below is based on an SDK so it is uses some functions that you may not have seen.

string userc = GetContextInfo("User", "UserId");
string tools = Dispatch.EitherField("selectedTools");
tools.Split(',');

string pID = Dispatch.EitherField("key16");

Record recRelTool = new Record("RelatedTools");
recRelTool.SetField("rato_CreatedBy", userc);
recRelTool.SetField("rato_Status", "active");
recRelTool.SetField("rato_server", pID);
recRelTool.SetField("rato_tool", tools);
recRelTool.SaveChanges();
Dispatch.Redirect(Url("1453"));

Where the ("rato_tools", tools) needs to be one of the tool id's in the value I have. I need to loop through until all of the tool id's have been used.

Was it helpful?

Solution

The call to split does not split your string, it returns an array of strings. You need to enumerate through this array to use one tool id at a time. Try the following:

string userc = GetContextInfo("User", "UserId");
string tools = Dispatch.EitherField("selectedTools");
string[] toolIds = tools.Split(',');
foreach (string toolId in toolIds) 
{
  Record recRelTool = new Record("RelatedTools");
  recRelTool.SetField("rato_CreatedBy", userc);
  recRelTool.SetField("rato_Status", "active");
  recRelTool.SetField("rato_server", pID);
  recRelTool.SetField("rato_tool", toolId);
  recRelTool.SaveChanges();
}

Dispatch.Redirect(Url("1453"));

OTHER TIPS

you have to assign your split return value:

var splitted = tools.Split(',');

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

and then you can iterate the collection:

foreach(string item in splitted)
{
//do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top