How to check if a particular column is empty in DataTable (Without using For Each Row)?

Without using For Each Row loop, How can we check if a column is empty or not in DataTable ??
I want to delete a column if it is empty and I acheived it with the help of For Each loop. Can anyone help me in solving this without loop?

In If Condition Check
DataTable.Rows(0)(“columnname”) is DBNull.Value then Delete a column

for each column in dt
if dt.Select(dt.Columns(index or columnname)=dbnull).Length=dt.Rows.Count
then msgbox “column is empty”
end if
next

@KinjalRD @Atrimal Thank you! I already tried this way but not worked.

error : DBNull is a type and cannot be used as an expression.

Can you please check again?

Thanks!

Hi Santhosh,

You can try this out.

5 Likes

Check if,

DataTable.Rows(0)(“columnname”).ToString = “”

2 Likes

dt.Select(dt.Columns(index or columnname).tostring=“”).Length=dt.Rows.Count

@RupeshGonte Thank you!

DataTable.Rows(0)(“columnname”).ToString = “”
this will check only for the first row , I need it for whole data table without using any loop. can you try ?

1 Like

You can try using below linq query
(From row In dt where row(“id”) isnot DBNull.Value).CopyToDatatable

Or You can assign it to array of DataRow

datarow arr = (From row In dt where row(“id”) isnot DBNull.Value).toArray
and check
if( arr.count=0)

1 Like

@SantoshPothina PFB

Perfect!

Hi, How to check particular column is null or empty, If column is not empty. Then how to get that value present in column. Please provide solution for this?
Thanks in Advance

How to check datatable is null or zero rows .we need to exclude two columns as they have values in it.
Any suggestions please
@vvaidya @Bharat_Kumar @Palaniyappan @NIVED_NAMBIAR @lakshman @ClaytonM

@Satish_Ch

Try below expression.

                  outputDT = InputDT.DefaultView.ToTable(False,"Column 1","Column 2",...."Column N")

Mention all columns except those 2 columns which contains values. And then check Rows count as below.

              IF outputDT.Rows.Count > 0
              Then 
                    Rows exists
               Else
                     Rows doesn't exists
1 Like

This expression still giving me count…is it instead of fals…do we have to type true in expression in my case

@Satish_Ch

Write OutputDT into Excel file using Write Range activity and check what data it is giving as output.

1 Like