How to check if a data table column contains any values?

Hi all,

How do I check if a data table column contains any values?

1 Like

Scenario
1.Use Foreachrow activities.
2.Iterate thru foreachrow and use if condition activites.
3.row(“Columname”).Tostring =“” if true then do your thing else do nothing.

@rohangroombridge

cheers :smiley:

Happy learning :smiley:

1 Like

Hi, Thank you for replying. Is there a way to do it without a for loop?

1 Like

yes it has.
DataTable.Rows(0)(“columnname”).ToString = “”

@rohangroombridge

cheers :smiley:

Happy learning :smiley:

1 Like

Hi,

You can check it by the following single sentence.

hasValue = dt.AsEnumerable.Select(function(r) r("ColumnName").ToString="Value").Contains(true)

Regards,

3 Likes

@rohangroombridge

Yes we can do that. Use below LINQ query.

int nullCount = yourDT.AsEnumerable.Where(Function(row) row("columnname") is DBNull.Value or Convert.Tostring(row("columnname")).Trim = "").CopyToDataTable.Rows.Count

If it is greater than zero then it contains null values else it doesn’t contain.

1 Like

Hi @rohangroombridge,

You can apply a filter on the datatable column like remove the datarows with null as column value and count the rows. Attaching the screenshot

image

You can check the following way if you have rows in the DT after removing empty rows

To loop through each column in a DataTable, check if a column contains any values, and capture the cell addresses of non-empty cells, you can use the following steps:

Sequence:
- Read Range activity (Excel/CSV) to read data into dtData
- Assign activity:
- Initialize cellAddressesList as New List(Of String)
- For Each activity (TypeArgument: DataColumn) to loop through dtData.Columns
- Assign activity:
- To: columnHasValues (Boolean)
- Value: dtData.AsEnumerable().Any(Function(row) Not row.IsNull(currentColumn.ColumnName) AndAlso Not String.IsNullOrEmpty(row(currentColumn).ToString()))
- If activity:
Condition: columnHasValues = True
Then:
- Assign activity:
- To: nonEmptyCellAddresses (IEnumerable(Of String))
- Value: dtData.AsEnumerable().Where(Function(row) Not row.IsNull(currentColumn.ColumnName) AndAlso Not String.IsNullOrEmpty(row(currentColumn).ToString())).Select(Function(row) currentColumn.ColumnName + (dtData.Rows.IndexOf(row) + 2).ToString) ‘ Adjust cell address format as needed
- Assign activity:
- To: cellAddressesList (List(Of String))
- Value: cellAddressesList.Concat(nonEmptyCellAddresses).ToList()
- Use cellAddressesList as needed (e.g., Log Message activity)