views:

63

answers:

2

Hi All.

I've a general question - Lets say I've an SQL Server 2008 DB , and it has some nullable columns. Some of these are null and some are not.

Next, I query the DB for a DataRow in C#.

What value would these null fields have in the DataRow object? I'm seeing that they have C# "null" values, but someone here said that DbNull != C# null....

Please explain... thanks.

+3  A: 

DBNull.Value which is not the same as null.

DBNull.Value Field (System)

Justin Niessner
What would happen if I were to write row.Field1.trim() for example?
Roey
@Roey - My guess is an InvalidCastException because you can't cast DBNull.Value to string.
Justin Niessner
A: 

With untyped datasets (in DataRow), you can test like this: DbNull.Value.Equals(dataRow["column"])

With typed datasets, for nullable columns, there are IsNull methods generated, e.g. myTypedDataRow.IsMyColumnNull() which by default you should test before trying to access actual myTypedDataRow.MyColumn value, because if it has a null value, it would throw an exception. This behavior can be changed by setting column's NullValue property in DataSet Designer.

František Žiačik