tags:

views:

45

answers:

2

Hi,

I am using WCf service in my windowsApplication... I got the exception in my client When i am trying to add items in Listbox by using ForEach Loop... The exception is "Collection was modified enumuration may not execute". How shall i solve this exception....

And my code is,

            foreach (ClsPC pc in iclsobj.GetPC())
            {
            if (listBox1.Items.Count == 0)
            {
            listBox1.Items.Add(pc.IPAddress);
             }
            else
            {
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
            if (!listBox1.Items[i].ToString().Contains(pc.IPAddress))
            {
            listBox1.Items.Add(pc.IPAddress);
             }
          }
        }
       }
 client.Close();

client = null;

}

+3  A: 

You're trying to modify the collection in you list box while looping through it (the inner loop), which isn't really such a hot idea. You'd be better performing this in your data select (i.e. a get distinct PC), or limiting the returned list, possibly something like:

var pcs = iclsobj.GetPC().Select(pc => new { IPAddress = pc.IPAddress}).Distinct();

And then just binding your listbox to this.

Paddy
Thanks for Your Code....i tried your code but i got different line... (ie)System.Linq.Enumerable+<DistinctIterator>d__7a`1[<>f__AnonymousType0`1[System.String]]now how can i get that IPAddress
Suryakavitha
now how shall i add that pcs in listbox
Suryakavitha
I can't make a whole lot of sense of the text in your first comment, but as for the second, it's as simple as how you add any enumerable to a collection. Personally, I'd set your filtered list as the datasource and then call databind...
Paddy
Sorry i dont know how to to bind that value.... can you tell me how to do?
Suryakavitha
Book might not be a bad idea at this stage...lstBox1.DataSource = pcs;lstBox1.DataBind();
Paddy
i cannt get lstbox1.Databinding() option... lstbox1 does not contain DataBinding function...
Suryakavitha
i used windowsforms so that i couldnt get Databind() function... Now tell me one thing that i have to get the IPlist as List<> instead of Var... what shall i do for this?
Suryakavitha
In your code i got some value in pcs Which i mentioned first post.. In that line containing the ipaddress list in resultview option.. Now how shall i get that result view.... Tell me..
Suryakavitha
It should be an enumerable list - you can use code similar to what you first posted - loop through it (foreach (var ipAddress in pcs) {...}) and add it to your list that way.
Paddy
Thans for nice code.... I used (var ipaddress in pcs) line i got the ipaddress but it comes as repeatly when i click the buttton...
Suryakavitha
A: 

you adding an item to the collection that you are looping through. Add the item to a temporary collection and then assign the collection items to the litsbox items after your foreach but still within the else

Leom Burke
i cant get ur point.. can you plz tell me briefly?
Suryakavitha
instead of what you are doing add the IP addresses to a separate list of strings. You can then bind the listbox to this specific list of strings when you finish the foreach loop. Note that I think Paddy's solution is better, however.
Leom Burke