How to count rows in DataTable?

datatable
studio

#1

Hi,

I have problem with getting a count of the number of rows in a data table, the Data Table is created from a Data Scraping.

Even if the Data Table is empty (I checked it for sure by export to csv file), DataTable.Rows.Count.ToString shows 1 row.
Also if Data Table has one row, counter still shows 1.

I tried with indexes, DataTable.Rows.IndexOf(row).ToString, with empty list Index = 0, with one row Index = 0, with two rows there are three indexes 0, 1 and 2.

Is there another way to count DataTable rows ? (I would like to avoid the solution of writing to csv file and then counting number of rows from it :slight_smile: )

Mac


#2

create a variable counter
initialize the counter in 0 before the for each
inside the for each increment counter with assign activity counter = counter+1


#3

Even if the DataTable has no values and returns nothing to csv file, the for each loop starts. I put DataTable.Rows.IndexOf(row).ToString inside For each and with empty list I’ve got index =0 and with one row list index also equals 0. For number of rows greater than 1 indexes are OK, from 0 to DataTable.Rows.Count for example two rows: 0,1,2. Three rows: 0,1,2,3 etc. I’ve got problem only with identify if there is one or none rows in DataTable.Thanks for the answer but It will not works.


#4

What does OutputDataTable say?


#5

OutputDataTable returns something, if I put OutputDataTable result into string and MessageBox, I see nothing but the len(string) = 4. String.IsNullOrEmpty(string) and IsNothing(string) does not work. Now I can check the len statement of OutputDataTable, of course if it will be always working ? Thanks :slight_smile:


#6

Output data table returns following:
If data table empty (returns a column(as default header) and count 1)
If data table has one row (returns first item with count 1)


#7

Hi Mac,

Have you tried deleting empty/Blank datarows and then trying to count the number of rows? After the removal of empty rows, you should get the row count correct.

Regards,
Amrita


#8

Hi @Amrita_Mogali

The above points made by reading empty sheet.(used Read range activity).


#9

Hi Amrita, how to identify and delete empty/Blank rows ?

In conclusion with empty list:

  • DataTable.Rows.Count = 1
  • OutputDataTable returns len(string) = 4
  • String.IsNullOrEmpty(string) and IsNothing(string) does not work
  • In For each row loop, loop starts but row.Item(0).ToString = IndexOutOfRange Error, the same error with row.Item(1).ToString
  • export DataTable to csv gives empty file (working with list of one row, DataTable.Rows.Count = 1 and csv have two rows, one with column names and datarow)

thanks
Mac