How to add a row from a datatable to another datatable

datatable

#1

I’m trying to Iterate with a datatable and for each row add that row to another datatable.

You can reproduce it simply by:
Create a for each row in a dt and add that row to another dt.

12

And I’m getting this error: " Add data row : Object reference not set to an instance of an object."

Anybody knows why?


#2

Probably forgot to initialize your dtFinal variable?

dtFinal = new DataTable() ?

The above solution might not work. You can try below ways.

image

image

image


#3

Thank you so much!
I have another doubt now and if you can help me it would be great. How can i change the value of an item of a row?
I’m making as assign like this : ASSIGN --> dt_Intermedium.Rows(0).Item(5) --> SUM_NewOrder.ToString

Is it possible to do it this way right?


#4

Yes sir.

or

dt_Intermedium.Rows(0)(5)


#5

Ok, it’s working fine here. Buuuuut, (sorry for my stupidity but I just can’t seem to pass trought this) now i’m getting this error on the same place as the first issue: “This row already belongs to another table.”. I can’t figure out what’s happening…


#6

Are you using one of the 2 methods mentioned in the Post 2?