Proper/Better Technique

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Hello, I have tried a couple of techniques and the ones I have tried have
failed and the ones that work, I don't like, so I thought I would see if there
is a better way of doing this.

I am attempting to copy a variable number of records in a dataview, change the
"keys" to a new value and insert those new records back into the underlying
DataTable.

I have attempted to create a DataView of the records that I am interested in
and looping through those records using a "For Each/Next Loop", but after the
first update is made I get an error message indicating that the collection has
been modified and I an unable to use enumeration". The only technique I have
found was to create a array of rows, and place reference to each row in this
array and then loop through the array and copy the record and insert back into
the underlying table. There must be a better way....Is there?
 
Try changing the FOR EACH to a standard FOR loop and using the index as,
well, an index into the DataView. Something like

Dim idx as index
Dim dr as datarow

For idx = 1 to dv.Count
dr = dv.Item(idx)
... do your stuff
Next

You may need idx = 0, or (dv.Count - 1) in the first line of the For. I can
never remember <g>

HTH

--
Rebecca Riordan, MVP

Designing Relational Database Systems
Microsoft SQL Server 2000 Programming Step by Step
Microsoft ADO.NET Step by Step

http://www.microsoft.com/mspress

Blessed are they who can laugh at themselves,
for they shall never cease to be amused...
 
Ah. I didn't consider the possibility that the new record would meet the
filter criteria on the dataview. Sorry about that.

Let me see here...you're copying the records, changing them, and then
re-inserting them, right? How about copying them into a separate dataset
and then merging it with the original?

--
Rebecca Riordan, MVP

Designing Relational Database Systems
Microsoft SQL Server 2000 Programming Step by Step
Microsoft ADO.NET Step by Step

http://www.microsoft.com/mspress

Blessed are they who can laugh at themselves,
for they shall never cease to be amused...
 
Back
Top