How to add a row from a datatable to another datatable

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.


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

Anybody knows why?

Probably forgot to initialize your dtFinal variable?

dtFinal = new DataTable() ?

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




1 Like

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?

Yes sir.



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…

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