Null Check in a Data Table


#1

Hello

I need to check whether a DataTable consists of any null Values?

Expected Result: Boolean Value (True or False)
DataTable : We Know only the Name of the DataTable, Say “DT”, NOT Sure How many Columns and NOT Sure How Many rows it has.
Logic to Use : Without Using Loop.

I just wanted to know whether we have any Null Value inside that DataTable or Not.

Thanks in Advance
Dheeraj.


#2

Hi you can use select method and pass the column name if you aware else the column index: like

DataRow rows = DataTable.Select("[COLUMN 1]=’’ OR [COLUMN 2]=’’");


#3

Hi @Divyashreem thanks for your help.

But I’m not exactly looking for this.
As I specified the requirement : We Know only the Name of the DataTable, Say “DT”, NOT Sure How many Columns and NOT Sure How Many rows and NOT Sure the Names of those Columns. DT is a Dynamic DataTable which will be created according to the Scenario.


#4

Hi @dheeraj_reddy97

Please try this:
“dt1.AsEnumerable().Where(Function(row) row.ItemArray.Any(Function(v) TypeOf v Is DBNull)).ToList().Count()”

Replace dt1 with your datatable. Basically this will return the number rows has null values
Use if condition and see if count is greater than 0 then it has null value.

Regards,
Sasikumar


#5

@dheeraj_reddy97

Try something like this.
DT.AsEnumerable().Where(Function(r) r.ItemArray.Any(Function(a) string.IsNullOrEmpty(a.ToString))).Count>0

Should return True of False


#6

Thank you guys @rajeev85 @Sasi.lalo… Its Working!!! :slight_smile: