27

My data table filled from db is having empty values in some cells.

The results database SP return has Null in them but in DataTable these values are appearing as '' or empty cells.

Please guide me how to use Select() to select these dbnull/ empty rows.

Thanks

2
  • 1
    Any specific column? Or you want to select rows with any column of NULL or empty? Commented Feb 17, 2012 at 7:01
  • I want to select with pId column which is an int. Commented Feb 17, 2012 at 7:06

2 Answers 2

103

The correct way to check for null is to check for it:

DataRow[] myResultSet = myDataTable.Select("[COLUMN NAME] is null");
Sign up to request clarification or add additional context in comments.

6 Comments

I've seen this answer in a lot of place but no reference to where it is documented. DataColumn.Expression is actually the only refernce I found and doesn't talk about it. Is it documented anywhere ?
Are the '[' and ']' necessary? If so, why? Thanks in advance!
@daro because the column name has a space
this is the only answer worked for me(tried [COLUMN NAME] ='' AND [COLUMN NAME] ='"+DBNull.Value+"' )
this should be the proper answer for the question
|
7

For one column

DataRow rows = DataTable.Select("[COLUMN 1]=''");

For more than one column

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

4 Comments

Does this actually work for both null and empty values? I have tried these and is not working for me. I have both null and empty values in the cells.
this only detect blank value and not null.
Ok for text columns, but beware. This will throw a System.Data.EvaluateException for datetime columns. "Cannot perform '=' operation on System.DateTime and System.String."
This does not detect null. This detects empty string. See James McG's answer for correct solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.