Adding a row to an existing datatable

How do i add a row from one datatable to another?

I am looping through a for each in a datatable of names taken from a patient scheudle (dtPatientSched)

i use one name at a time to enter on a web page

I am going through a for each loop in datatable names scrapped from a webpage (dtWebNames).

if the patient’s name is on dtWebNames i add it the entire row of that patient in dtPatientSched to dtFoundPat. if the patient’s name is not found then I want to add it to the database dtNotFoundPat.

then i create a CSV for each dtFoundPat and dtNotFoundPat.

i am stuck copying the appropriate row from dtWebNames to the dtFoundPat and dtNotFoundPat tables.

i have tried Add new row and at best i get the error. This row already belongs to another table.

arrayrow i have tried numerous variations on variable of datarow types arrays, etc

datarow i leave blank

datatable dtFoundPat

i do not know what i am doing wrong please be very explicit

also i have looked and played with invoke method, but i do not understand how to use it yet.

thanks

1 Like

You can’t change the datatable you are iterating through while you’re iterating through it. Instead, you have to make a copy of the current row to put into the new datatable.

If the datatables you are copying have the exact same format, then you can use an add datarow activity and in the array row argument put in row.ItemArray

Another way is to use invoke method. The TargetObject is the datatable you want to copy to. The MethodName is ImportRow. The Paramaters needs to be ‘In’ of type DataRow and should be row (or whatever you nanmed the variable in your for each row loop)

4 Likes

I’m not changing a table i am iterating through. i am reading the current row then writing it to 3rd datatable. i will try it with row.itemarray. i do have a nested for each so i will double check i don’t have any other tables getting read and written to at the same time.

with the invoke method are the MethodNames already loaded? i don’t get any “pre populated” results when i type there.

thanks

1 Like

But if you supply the row variable that you are iterating through, it is trying to move that row to the new table. Instead, you need to Import it (which is copying it) or to copy the contents from it (using row.itemarray) and paste it into the new table.

There aren’t any preloaded methodnames that pop up with intellisense. You are invoking the ImportRow method as seen here though DataTable.ImportRow(DataRow) Method (System.Data) | Microsoft Docs

2 Likes

holy crap thank you for explaining the invoke method feature. people kept skipping key points on that.

that seems to be working sort of, but now I’m getting input string was not in a correct format.couldn’t store <> patient cell column expected type is decimal. i’ll open a separate thread for that.

thanks!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.