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 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?
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…
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.