Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete records from dataset. 1

Status
Not open for further replies.

160473

IS-IT--Management
Apr 16, 2003
55
FI
I have a table in a dataset. I need to delete several rows in that table. But if I do like this:

For Each row In newDataTable.Rows
If row(1) = 505050 Then
newDataTable.Rows(linenr).Delete()
End If
linenr = linenr + 1
Next

then the line count changes and give me the following error:
An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Additional information: Collection was modified; enumeration operation may not execute.

Is there an alternative way of deleting rows in a dataset?
Like the SQL-way; 'DELETE FROM table WHERE ID=505050'
 
I would do it like this

Code:
dim intcounter as integer
dim inttemp as integer
intcounter = newdatatable.rows.count
inttemp = 0
do while intcounter > 0
  If newdatatable.rows(inttemp).item(1) = 505050 Then
    newDataTable.Rows(linenr).Delete()
  else
    inttemp += 1
  End If
  intcounter -= 1
loop

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thanks!!!
You are the star of .NET!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top