Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a table with a DateTime column the column can have NULL values

Now I connect to the database using an ODBC connection and get the value into a DataTable in .net / c#.

I am able to check it for NULL by going

if(String.IsNullOrEmpty(table.rows[0][0].ToString()) {      //Whatever I want to do } 

Is String.IsNullOrEmpty the correct way to check for null values.

like image 723
soldieraman Avatar asked Jan 07 '10 01:01

soldieraman


People also ask

How check DataRow column is empty C#?

foreach(DataRow row in dataTable. Rows) { if(row. IsNull("myColumn")) throw new Exception("Empty value!") }

How do you handle database null values in C#?

IsNullOrEmpty() Method of C# If any string is not assigned any value, then it will have Null value. The symbol of assigning Null value is “ “or String. Empty(A constant for empty strings). This method will take a parameter that will be of System.


1 Answers

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    } 
like image 178
jball Avatar answered Sep 23 '22 17:09

jball