Dealing with Database Null

  • Thread starter Thread starter Rene
  • Start date Start date
R

Rene

I have my object model thing going with some classes that contain properties
that are saved into a database. Some of these properties are *numeric*
properties that can be left Null in the database table.



My problem is that there does not seem to be a way to assign a Null value to
your typical numeric value type variable.



Other than using the sql variables types (which cant' be serialized), can
anyone share some practical solutions on how to go around this limitation?
In other words, how do you guys deal with numeric properties that can be
Null.



Thank you.
 
Rene said:
I have my object model thing going with some classes that contain
properties
that are saved into a database. Some of these properties are *numeric*
properties that can be left Null in the database table.



My problem is that there does not seem to be a way to assign a Null value
to
your typical numeric value type variable.



Other than using the sql variables types (which cant' be serialized), can
anyone share some practical solutions on how to go around this limitation?
In other words, how do you guys deal with numeric properties that can be
Null.

I implement IsMyPropertyNameNull and set this when I am reading from the
datareader. I use a static null checker function that I pass the reader
value and the IsNull property to it. This avoids trying to cast null as a
numeric value and also sets the IsNull to true.

In the setter for the numeric property I set isNull to false.

I do the opposite when writing back to the database. If IsNull is true then
i use DbNull.Value as the parameter value.

HTH

SP
 
Hi Rene,

This has been discussed here several times, so you can check the archives
for more info.

basically you have two options:
1- Use a wrapper type

2- Use a special value of the type as a null indicator, for exampe I use
DateTime.MinValue to indicate a datetime with no value set in one
application, of course I do so cause that value has no meaning on the
application, if not I cannot use this approach.


Also note that in .NET 2.0 this will change.

Cheers,
 
Back
Top