How to copy from Collection of DataRows to DataTable if Collection is empty?

Hey masters,

How can I copy from a Collection of DataRows to a DataTable if my collection is empty?
If the collection is not empty, it works with this assign: newPackagesCollection.CopyToDataTable, where newPackagesCollection is a System.Data.EnumerableRowCollection<System.Data.DataRow>. But it fails when the collection has no rows…
And when I try this, it also fails: if(newPackagesCollection.Count > 0,newPackagesCollection.CopyToDataTable, new System.Data.DataTable)

Assign: Can not assign ‘if(newPackagesCollection.Count > 0,newPackagesCollection.CopyToDataTable, new System.Data.DataTable)’ to ‘newPackageDT’.

Pedro Pais

Hi @Pedro_Pais

Please change the assign variable type to ‘DataType’ and try this method. Thanks.

Happy Automation.

Feel free to reach us at any time if you have doubts.

Hi @vignesh.ks

I can’t assign to DataType, it doesn’t work.

@Pedro_Pais Does the empty collection have the column information? How are you generating/getting the the Collection of data rows? And what should be done if the Collection is empty?

Try this -

newPackageDT = if(newPackagesCollection.Count > 0, newPackagesCollection.CopyToDataTable, Nothing)

Even this will also work

newPackageDT = if(newPackagesCollection.Count > 0, newPackagesCollection.CopyToDataTable, New DataTable())

and if you want to retain the column information of a Data Table you can use this

newPackageDT = if(newPackagesCollection.Count > 0, newPackagesCollection.CopyToDataTable, exampleDT.Clone)

Important : Set Variable Type of newPakageDT as System.Data.DataTable

if you know the name of the columns of this collection, first set them. Try to add the rows after that.
It should resolve the issue.

Hi @Pedro_Pais

I tried this code in my Studio, I didn’t get any issue. Attached screen shot for your reference.

Correct me if I’m doing any mistake. Thanks.

Happy Automation

I’m using this assign from the image to get new elements from comparing two existent datatables to an EnumerableRowCollection<System.Data.DataRow> and then copy to a new Datatable.

It works very well if there are new elements. But if the resulting Collection is empty, it doesn’t work, because there are no elements to copy to a dataTable.

I tried all the options @kumar.varun2 wrote here, before, and before copying to a DataTable, but none worked! The error appears in execution time.

I wanted to know how I can see if an EnumerableRowCollection<System.Data.DataRow> is empty before copying to a datatable… I can’t find a solution.

My solution: I put the code into a Try-Catch, and if an InvalidOperationException occurs, it says there are no new rows to copy. Not the best, but it works…

I separated both variables in two different assigns, to see in which moment it breaks… And the message error is different now:

Error: Assign: Activity ‘1.103: VisualBasicValue’ cannot access this public location reference because it is only valid for activity ‘1.109: VisualBasicValue<EnumerableRowCollection>’. Only the activity which obtained the public location reference is allowed to use it.

What is that?

UPDATE: Now I see it must be inside a Multiple Assign to work…

Suggestion: Try first to assign the result to an array of rows.
Then if the array has more than 0 rows, use the copy to data table function.

That’s the easiest way I found to filter a table and handling the null values.
I sometimes want to do everything in a single line, but VB just does not allow me to do it, lol.
Hope this helps, let me know.

Thank you, @GersonTun , well thought! It worked!

Pedro Pais

1 Like

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