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.

4 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