Search code examples
.netdatatablestrongly-typed-datasetdatacolumn

Can I configure a strongly typed data set to use nullable values?


If I have a strongly typed data table with a column for values of type Int32, and this column allows nulls, then I'll get an exception if I do this for a row where the value is null:

int value = row.CustomValue;

Instead I need to do this:

if (!row.IsCustomValueNull()) {
    int value = row.CustomValue;
    // do something with this value
}

Ideally I would like to be able to do this:

int? value = row.CustomValue;

Of course I could always write my own method, something like GetCustomValueOrNull; but it'd be preferable if the property auto-generated for the column itself simply returned a nullable. Is this possible?


Solution

  • Unfortunately, this is not supported.

    However, you can make your own wrapper property, like this:

        public int? CustomValue {
            get { return IsCustomValueqlNull() ? new int?() : CustomValueSql; }
            set {
                if (value == null)
                    SetCustomValueSqlNull();
                else
                    CustomValueSql = value.Value;
            }
        }
    

    Where CustomValueSql is the actually column name.