Data Table is nothing

I am extracting a data table from a website which may or may not have a table. When a table exists, it is extracted to PTDataTable, saved to an excel file, and processed using PTDataTable.Rows(n).Item(n).ToString. That all works perfectly.

I am attempting to handle the condition where there is no table by using an Else If conditioned with (not PTDataTable is nothing); however the condition is ignored when there is no table. I set a breakpoint on the Else If and step through. It drops right into the body as if PTDataTable exists, then issues error “There is no row at position 0” on the Else If condition which checks the first cell in the table.

The image shows the sequence and the status of PTDataTable at the breakpoint (first Else If) which shows Columns and Rows to be empty:

I found the “(not PTDataTable is nothing)” construct in the solution for a similar question but it clearly is not working here. The PTExit check is part of a Do While which permits several delayed attempts when there is no table found.

Hey @EarlA

You can try these below conditions in if

PTDataTable.Rows.Count = 0

If the above condition didn’t work then u can use
PTDataTable.IsEmpty()

If the above 2 conditions didn’t work then u can use
String.IsNullOrEmpty(PTDataTable.ToString())

I have given you 3 solutions,

Hope it helps you
Happy automation!

Hi @EarlA

Please add a condition to make sure that the data table is empty or not by using this expression:

yourDt.Rows.Count = 0

If the count is 0, that means the data table has no rows.

Hope this helps,
Best Regards.

@EarlA
Also u can use linq for checking
Below is the linq code

In assign activity: isDataTableEmpty = (PTDataTable.AsEnumerable().Any()
The isDataTableEmpty should be a variable of Bool type

Then you can use the variable in if condition

Below is the explanation of Linq

The .AsEnumerable() method converts the PTDataTable into an enumerable collection, and the .Any() method checks if there are any elements in that collection. If the PTDataTable is empty, the .Any()` method will return False , and the isDataTableEmpty variable will be set to True

You can refer Below given file
Test.zip (8.3 KB)

Hope it helps you!
Happy automation

Hi @EarlA

After extracting the table by extract datatable activity.
Use the PTDatatable.rows.Count > 2 then it has the rows in the datatable check with the If condition.

Hope it helps!!

Hi @EarlA

Check the Row Count of the datatable ->PTData Table.Rows.Count>1 instead of not PTDataTable is nothing

Hope it helps!!

Thank you to all who offered suggestions. Checking Rows.Count did the trick.

Hey,

You can try this condition,
String.IsNullorEmpty( yourvariable)

Hope it helps you.

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