: an exception raised

G

Guest

Hello,

Dim row As DataRow
Dim column As DataColumn
For Each row In myDTSrc.Rows
For Each column In myDTSrc.Columns
myRowDest(column.ColumnName.ToString) =
row(column.ColumnName.ToString)
Next
myDTDest.Rows.Add(myRowDest)
myDA.Update(myDTDest)
myDTDest.AcceptChanges()
Next

This code adds first row from MyDTSrc to myDTDest which is attached an SQL
table, but at the second row I get exception "This row already belongs to
this table", what is problem?
Thanks,
Jim.
 
J

jlien

Try this:

Dim row As DataRow
Dim column As DataColumn
For Each row In myDTSrc.Rows
myRowDest = myDTDest.NewRow
For Each column In myDTSrc.Columns
myRowDest(column.ColumnName.ToString) =
row(column.ColumnName.ToString)
Next
myDTDest.Rows.Add(myRowDest)

Next
myDA.Update(myDTDest)
myDTDest.AcceptChanges()
 
K

Karl Seguin

Jim,
The error pretty clearly states what the problem is. A datarow can only
belong to 1 datatable....remember all these are references so all you are
copying is the reference to the same datarow...

Why not just do:

myDtDest = myDtSrc.Copy(); ??

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
G

Guest

Thanks Karl, I did,
myDTDest = myDTSrc.Copy()
myDA.Update(myDTDest)
but I do nto see data in table. What might be the problem?
 
G

Guest

Thanks Eliyahu,
I did this, and I am wondering if this will cause any overflow problem
because of a new decleration for each row. It might be a big table.

Dim row As DataRow
Dim column As DataColumn
For Each row In myDTSrc.Rows
Dim myRowDest As DataRow = myDTDest.NewRow
For Each column In myDTSrc.Columns
myRowDest(column.ColumnName.ToString) => row(column.ColumnName.ToString)
Next
myDTDest.Rows.Add(myRowDest)
myDA.Update(myDTDest)
myDTDest.AcceptChanges()
Next
 
E

Eliyahu Goldin

This is how it HAS to be. Every row HAS to be a separate object.

Eliyahu
 
G

Guest

I am new in asp.net, so if there is 5000 object is created in that function,
will it cause any problem? I assume the object life will end when the
function ends.
 
E

Eliyahu Goldin

It should not be any different from reading 5000 rows from a database.When
you read 5000 rows from a database, every row is represented by a separate
object. When you change one row, you wouldn't like another row to get the
same changes would you? I am not aware of any special limitation for tables.
Usual memory limitations apply. 5000 rows is not a very large table, should
be ok. Depends on row content of course.

Since you attach row objects to a table and the table persists outside the
function, the row objects will persist too.

Eliyahu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top