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:
ipList = ds.Tables("RouterIPs").Copy
Dim x As DataRow
For Each x In ipList.Rows
If x.Item("IP_Addr") Is DBNull.Value Then
ipList.Rows.Remove(x)
End If
Next
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
Doing it as a regular loop and going from the end to the beginning should work.
// C# cause I don't know VB.Net
int iCount = ipList.Rows.Count;
for( int i = iCount-1; i >=0; i-- ){
DataRow x = ipList.Rows[i];
// check IP_Addr for nullness. If null remove it
}
You could also try removing the NULL rows in the original query. Something like:
-- Sql Server
SELECT IP_Addr from SomeTable
WHERE IP_ADDR IS NOT NULL
You might be able to try that filter in a DataView but I'm not sure if "IS NOT NULL" is supported by the DataView's rowfilter.
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:
ipList = ds.Tables("RouterIPs").Clone
'add only non-null ip_addr dropdownlist view
Dim i As Integer
Dim dr As DataRow
For i = 0 To (ds.Tables("RouterIPs").Rows.Count - 1)
If Not ds.Tables("RouterIPs").Rows(i).Item("IP_Addr") Is DBNull.Value Then
ipList.ImportRow(ds.Tables("RouterIPs").Rows(i))
End If
Next
nickicl
Star
8230 Points
1646 Posts
remove null rows in data table
Aug 13, 2003 01:29 PM|LINK
ipList = ds.Tables("RouterIPs").Copy Dim x As DataRow For Each x In ipList.Rows If x.Item("IP_Addr") Is DBNull.Value Then ipList.Rows.Remove(x) End If Nextthe 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! NicMcMurdoStati...
Contributor
3015 Points
601 Posts
Re: remove null rows in data table
Aug 13, 2003 02:26 PM|LINK
// C# cause I don't know VB.Net int iCount = ipList.Rows.Count; for( int i = iCount-1; i >=0; i-- ){ DataRow x = ipList.Rows[i]; // check IP_Addr for nullness. If null remove it }You could also try removing the NULL rows in the original query. Something like: You might be able to try that filter in a DataView but I'm not sure if "IS NOT NULL" is supported by the DataView's rowfilter.nickicl
Star
8230 Points
1646 Posts
Re: remove null rows in data table
Aug 13, 2003 04:45 PM|LINK
ipList = ds.Tables("RouterIPs").Clone 'add only non-null ip_addr dropdownlist view Dim i As Integer Dim dr As DataRow For i = 0 To (ds.Tables("RouterIPs").Rows.Count - 1) If Not ds.Tables("RouterIPs").Rows(i).Item("IP_Addr") Is DBNull.Value Then ipList.ImportRow(ds.Tables("RouterIPs").Rows(i)) End If Nextautofed
Participant
1789 Points
357 Posts
Re: remove null rows in data table
Aug 14, 2003 02:27 AM|LINK