Error on searching through a table for values


hey guys, i'm having problem updating table. gets information put every 5 seconds, different clients. checks if there client in table (it compares ip addresses) , updates if finds one, if not adds new row. when crashes error: "collection modified; enumeration operation might not execute". when searched around fix saw needed loop, cant think of how it, here relevant bit of code:

                if (clientinfo != null)
                {
                    foreach(datarow dr in clienttable.rows)
                    {
                        string strdatarowip = convert.tostring(dr[0]);
                        if (string.equals(clientid, strdatarowip))
                        {
                            dr.beginedit();
                            dr[1] = clientvalue1;
                            dr[2] = clientvalue2;
                            dr[3] = clientvalue3;
                            dr[4] = clientvalue4;
                            dr.acceptchanges();
                        }
                        else
                        {
                            this.invoke((methodinvoker)delegate()
                            {
                                clientinfo.rows.add(clientid, clientvalue1, clientvalue2, clientvalue3, clientvalue4);
                            });
                        }
                    }
                }

what guys think?

hi there,

you can try maybe this:

if (clientinfo != null)                 {                     datatable acopy = clientinfo.copy();                     acopy.clear();                     foreach(datarow dr in clienttable.rows)                     {                         string strdatarowip = convert.tostring(dr[0]);                         if (string.equals(clientid, strdatarowip))                         {                             dr.beginedit();                             dr[1] = clientvalue1;                             dr[2] = clientvalue2;                             dr[3] = clientvalue3;                             dr[4] = clientvalue4;                             dr.acceptchanges();                         }                         else                         {                          datarow rw = clientinfo.newrow();                         rw["clientid"] = clientid;                          .....                             rw["clientvalue4"] = clientvalue4;                             acopy.importrow(rw);                         }                     }                 }  foreach(datarow dr in acopy.rows) {  clientinfo.rows.add(dr["clientid"],....,dr["clientvalue4"]); }
because this:

   this.invoke((methodinvoker)delegate()
                            {
                                clientinfo.rows.add(clientid, clientvalue1, clientvalue2, clientvalue3, clientvalue4);
                            });

modifies maybe 'clienttable' table , in iteration can't modify list

hope helps.. :)


if code doesn't work, don't worry on way.. don't forget mark thread solved when done...



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'