Best Way to Check If a Data Table Has a Null Value in It

Check if a Data Table has a null value in it

This way:

dataSet.Tables.OfType<DataTable>().Any(x => x.Rows.OfType<DataRow>().Any(y => y.ItemArray.Any(z => z == null || z == DBNull.Value)));

It checks if any column in any row in any table in a dataset, has a null value.

This code will work in every scenario without any error.

Best way to check if column returns a null value (from database to .net application)

Use DBNull.Value.Equals on the object without converting it to a string.

Here's an example:

   if (! DBNull.Value.Equals(row[fieldName])) 
{
//not null
}
else
{
//null
}

check bound datatable for null value vb.net

use DbNull.Value:

   If Not DbNull.Value.Equals(dt.Rows(1).Item(1).value) Then 
' do something
Else
' do something else
End If

or use

If dt.Rows(1).Item(1).value=DbNull.Value

Need help Creating a Datatable from a list with possible null values

You need to set the table columns to accept nulls.

 DataColumn datecolumn = new DataColumn("DateCreated");
datecolumn.AllowDBNull = true;

I would put the Column names in an array and loop through them, ie something similar to this.

DataTable table = new DataTable();

string[] column = { "DateCreated", "DepthCode", "DepthDateCreated" };

foreach (var item in column)
{
DataColumn datecolumn = new DataColumn(item);
datecolumn.AllowDBNull = true;
table.Columns.Add(item);
}


Related Topics



Leave a reply



Submit