veena
veena

Reputation: 103

comma delimited string collection

CommaDelimitedStringCollection commaStr = new CommaDelimitedStringCollection();
string itemList = Convert.ToString(HIGList[i].AccountId) + '$' + "HIG" + ',' + '$';
commaStr.Add(itemList);
HigList = HigList + commaStr;

When i am trying to execute this it is showing error like

Value may not contain ','

Upvotes: 0

Views: 1360

Answers (2)

haim770
haim770

Reputation: 49095

CommaDelimitedStringCollection is intended to generate a comma delimited string. it means that you add values to it and when you call it's ToString() method, you get the values separated with a comma between each value.

That's why it won't let you add a value with a (non-escaped) comma , in it, as it violates it's very use.

For example:

var csv = new CommaDelimitedStringCollection();
var cities = new[] { "New York", "Log Angeles", "Toronto", "San Francisco" };

foreach (var city in cities)
{
    csv.Add(city);
}

Console.WriteLine(csv.ToString()); // will output: New York,Log Angeles,Toronto,San Francisco

And in your case:

CommaDelimitedStringCollection commaStr = new CommaDelimitedStringCollection();
string itemList = Convert.ToString(HIGList[i].AccountId) + '$' + "HIG" + ',' + '$';
commaStr.AddRange(itemList.Split(','));
HigList = HigList + commaStr;

Upvotes: 1

svinja
svinja

Reputation: 5576

The error message tells you exactly what the problem is and it is immediately visible from the code... You're trying to add a string that contains a comma to a comma delimited string collection. Obviously this makes no sense so an exception is thrown.

Upvotes: 0

Related Questions