Tuesday, 6 May 2014

C# - How to check if a column exists in a DataRow

It's a common requirement when you work with DataRows to check if a column exists. The next check would be if the column has a value which we will see afterwards in this post.

To check if a column exists:
dRow.Table.Columns["msisdn"] == null
The row doesn't contain a column property as such. Therefore you will have to get into the Parent table property to check into the columns in the row.

Note:
If the column doesn't exist,
dr["msisdn"] == null
will throw and error.

To check if a column has value and is not null value:
dr["msisdn"] == DBNull.Value
and
if(dr.IsNull("msisdn"))
are used to check if a column has value

No comments:

Post a comment