I have a datatable that is populated by reading in an excel file. That is working correctly. However, I have a while rows loop that loops thru the excel file and loads it into a datatable. Inside the loop, I am doing an if then else statement to check the first column header using this syntax:
out_DataTable.Rows(count)(“First Column Header Name”).ToString.Trim.Equals(“NAME:”)
However, I now have a second option I need to check for:
out_DataTable.Rows(count)(“Second Column Header Name”).ToString.Trim.Equals(“NAME:”)
When I add both of them as conditions using either: OR OrElse in the if then else statement. If the data I am sending thru matches the first condition. It works. If the data I am sending thru matches the second condition, it errors out. It never checks the second condition! I also tried an embedded If statement under the else to check for the second condition and it errors out there also.
This is how I have it coded currently:
If condition out_DataTable.Rows(count)(“First Column Header Name”).ToString.Trim.Equals(“NAME:”)
Then … it sets a variable
else
If condition out_DataTable.Rows(count)(“Second Column Header Name”).ToString.Trim.Equals(“NAME:”)
Then it sets the same variable as above.
Else
It sets a different variable.
What am I doing wrong? I also tried to use a Switch statement and that just caused UiPath Studio to crash.
The way you’re doing this is weird. You should check the “Add Headers” box in the Read Range activity so it sets the column names of the datatable properly. Then you can use out_DataTable.Columns as a collection to loop through and check the names.
Already tried that, if I execute with a variable that meets the first criteria (“First Column Header Name”) then it processes it, but if I run it with data that meets the second criteria (“Second Column Header Name”). Then it errors out with the error" If Column “SecondColumn Header Name” does not belong to the table DataTable. Which is wrong, it does meet the second OR condition (I also tried OrElse same results).
I also tried with just the first condition in the if statement thinking it would then go to the else statement. It does not, it gives the same error.
Why does it error out when it does not find it in the datatable? If it is false, it should just go to the else part of the code.
The add headers is checked in the read range activity. When I try out_DataTable.Columns(“Second Column Header Name”).ToString.Trim.Equals(“NAME:”) in the if statement instead of the rows query. I get the error: If Object reference not set to an instance of an object.
In the code, it does use a trim to remove the spaces, but it still results in this issue of it not evaluating the second condition and/or going to the else part of the if statement.
Your code looks for a column named “Second Column Header Name.” If you need the value from the first column and row then you do out_Datatable.Rows(0).Item(0)
Sorry for the confusion. I have two different excel spreadsheets, one with First Column Header Name as the first header in it. The second excel spreadsheet has Second Column Header Name as the first header in it. I am not comparing the two against each other. They are used in two different runs of the bot.
One other thing, I get the error when the second excel spreasheet with the header Second Column Header Name is going thru the if then else. In the if then else it first checks for First Column Header Name, then throws the error when it should go to the else part.
That’s because there’s no column named First Column Header Name so it errors. What you need to do is loop through out_DataTable.Columns and check the column names.
“I have a datatable that is populated by reading in an excel file.” so there is dt_1 and excelFile1.
“However, I have a while rows loop that loops thru the excel file and loads it into a datatable.”
Is this the original excel file (excelFile1) and dt_1?