For each row is getting errors!


I am facing some strange issues. I have a datatable in a for each row. I compare a value of this datatable with another datatable with other for each row. I don’t know why it’s failing, but I compare this values with .Equals method. Sometimes it works fine, but other times it fails. In fact, sometimes it fails but when I execute again the script, it works. It fails in the comparison.

I need help!!!

Hi @Samuel_Piqueras,

Can you share your xaml file I ll check and update you.


@Samuel_Piqueras using 2 for each activities to “join” information its never the best way… if you want do something like dt1.column0 = dt2.column1 and get all the results that fit that condition you should:

listDataRow = dt1.Rows.cast(of DataRow).Where(function(rowDT1) string.isnotnullorwhitespace(rowDT1) andAlso dt2.Rows.cast(of DataRow).Any(function rowDT2) string.isnotnullorwhitespace(rowDT2) AndAlso rowDT1(0).ToString=rowDT2(1).tostring)).tolist

If you need information from the second datatable you can do 1 for each and a Select…

In case you want to stay with your for each activity your error will be with nulls exception try to use if activity and string.isnotnullorwhitespace(xxx) to be sure that is not nothing or empty :slight_smile:

1 Like

Hi Ignacio,

Can you expand on why splitting that line into multiple activities is never the best way? My instinct would be to have multiple assigns for readability.


This is just my opinion, but if you can design a project that only takes a few activities, it will be more readable in terms of clutter. Like if you need to search through a long list of activities and into multiple sequences inside each other, then it becomes less readable.

But, like there needs to be a balance also, and it’s something I’m still working at myself. You don’t want many giant blocks of query functions or long dictionary references because then debugging becomes more tiresome. While, you also don’t want to break your logic up into every little activity (ie creating an If activity for every single condition needed)

If you can present a project in a small flowchart built by small sequence boxes that describes the process to a non-programmer then that’s good. If they need to migrate through many embeded For Eaches and If clusterbleeps, then that is bad :stuck_out_tongue:

An example of efficient processing, I believe, would be something like this:
—assuming you are wanting to compare tables and perform actions

    For each block (For each row in dt1.AsEnumerable.Where(Function(r1) If(IsNumeric(r1(0).ToString.Trim), CDbl(r1(2).ToString.Trim)>0, False) ).ToArray)
        If activity (condition: dt2.AsEnumerable.Where(Function(r2) If(IsNumeric(r2(0).ToString.Trim), CDbl(r2(2).ToString.Trim)=CDbl(row(2).ToString.Trim, False) ).ToArray.Count>0
            <perform actions>

.... and so on

So in that example, I could have run dt2 through another For each, but when dealing with thousands of lines of data this can use up memory and take a long time.

EDIT: Also, I could have assigned the datatable array before it, but I didn’t see a point in this case (not all cases) and it would be more confusing to have to look for what that array of rows represents. But on the other hand, if there is a coding error, you could check the array before the for each, so there are some disadvantages also. You would not want to assign each item from the row, though, because it’s unnecessary and adds bulkiness/clutter to the For each block.

Just my 2 cents


I don’t have access to the xaml right now, but tomorrow I’ll share it!

I would like to try this but I don’t understand it very well. What is the functionality of cast? How can I compare 2 dt and obtain new dt trough the comparison of 2 columns? I tried with the select method, but this dt doesn’t have headers! Thanks very much