Is it possible for properties to be null (at least those that can store a null value in the database)?
Although you cannot store NULL value types in a typed DataSet , you can force the columns of the data set to take DBNull values ββ( column.AllowDBNull = true; ) and create a couple of helper functions that will perform this translation for you.
public static T? DBToNullable<T>(object dbValue) where T: struct { if (dbValue == DBNull.Value) return null; else return (T)dbValue; } public static object NullableToDB<T>(T? value) where T: struct { if (value.HasValue) return (object)(T)value; else return DBNull.Value; }
Then they can be used as follows:
int? value = .... DataRow row = .... row["MyDataColumn"] = NullableToDB(value); value = DBToNullable<int>(row["MyDataColumn"]);
This should ease the pain.
If this is not so, is it designed in this way?
It was probably designed in this way for historical reasons that are related to the conceptual difference between the NULL database NULL (value: unknown value) and the C # NULL link (which means: this link has not been assigned yet). Of course, these values ββhave changed since how types of null values ββwere added in C #, but by then the DataSet had already DataSet .
source share