remove null rows in data tablehttp://forums.asp.net/t/307989.aspx/1?remove+null+rows+in+data+tableThu, 14 Aug 2003 02:27:20 -0400307989307989http://forums.asp.net/p/307989/307989.aspx/1?remove+null+rows+in+data+tableremove null rows in data table I have a datatable which currently contains many items which are null. A copy of this table is used as the datasource for a dropdownlist and I would like to remvoe those empty rows from the table. I though it would be simple to loop through the rows and check for null, if it's null remove it but I think it is causing a problem to alter the datatable within the for loop statement but I cannot think of any other way to do this. Please help. my code: <pre class="prettyprint">ipList = ds.Tables(&quot;RouterIPs&quot;).Copy Dim x As DataRow For Each x In ipList.Rows If x.Item(&quot;IP_Addr&quot;) Is DBNull.Value Then ipList.Rows.Remove(x) End If Next</pre> the error: Collection was modified; enumeration operation may not execute. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. Source Error: Line 275: ipList.Rows.Remove(x) Line 276: End If Line 277: Next Line 278: Line 279: dr = ipList.NewRow THANKS! Nic 2003-08-13T13:29:42-04:00308056http://forums.asp.net/p/307989/308056.aspx/1?Re+remove+null+rows+in+data+tableRe: remove null rows in data table Doing it as a regular loop and going from the end to the beginning should work. <pre class="prettyprint">// C# cause I don't know VB.Net int iCount = ipList.Rows.Count; for( int i = iCount-1; i &gt;=0; i-- ){ DataRow x = ipList.Rows[i]; // check IP_Addr for nullness. If null remove it }</pre> You could also try removing the NULL rows in the original query. Something like: <pre class="prettyprint"> -- Sql Server SELECT IP_Addr from SomeTable WHERE IP_ADDR IS NOT NULL </pre> You might be able to try that filter in a DataView but I'm not sure if &quot;IS NOT NULL&quot; is supported by the DataView's rowfilter. 2003-08-13T14:26:01-04:00308264http://forums.asp.net/p/307989/308264.aspx/1?Re+remove+null+rows+in+data+tableRe: remove null rows in data table Thanks for all the suggestions, McMurdoStation, I considered them all but I was also using this data for filling other thing which I would like to keep the nulls and I would to change the minimum amount of code possible so I took a different approach. Instead of removing the null rows I selectively added rows that were not null. Here is my solution: <pre class="prettyprint">ipList = ds.Tables(&quot;RouterIPs&quot;).Clone 'add only non-null ip_addr dropdownlist view Dim i As Integer Dim dr As DataRow For i = 0 To (ds.Tables(&quot;RouterIPs&quot;).Rows.Count - 1) If Not ds.Tables(&quot;RouterIPs&quot;).Rows(i).Item(&quot;IP_Addr&quot;) Is DBNull.Value Then ipList.ImportRow(ds.Tables(&quot;RouterIPs&quot;).Rows(i)) End If Next</pre> 2003-08-13T16:45:09-04:00308779http://forums.asp.net/p/307989/308779.aspx/1?Re+remove+null+rows+in+data+tableRe: remove null rows in data table Much more efficient would be to create a DataView for the DataTable, with the RowFilter set to &quot;IP_Addr &lt;&gt; NULL&quot;. AutoFed 2003-08-14T02:27:20-04:00