decimal - nulls

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

We have a SQL Server table with a decimal column that is not required.
We're building a .NET application for data entry. If the value is optional,
but a decimal is strongly typed in C# and cannot be assigned a value of
null, what are the best practices for dealing with passing this potentially
null value around in a Decimal C# datatype?

Thanks in advance.

Mark
 
This is more difficult, because a Decimal cannot have a null value - by
default it has a value of 0.

Now, when you are getting a value out of a datatable or datareader, you can
get either a DBNull, or an actual decimal value. The most common class for
these is object - so you could always pass around an object, and then always
have to test whether or not it is a DBNull, or an actual decimal.

SqlDataReader class (if you are using sql server) has a GetSqlDecimal method
which returns a SqlDecimal datatype. This structure is like the Decimal
structure, except it has the concept of a Null value.
 
We use DataType.MinValue to represent nulls. We define a constant called
something like Constants.NULL_DECIMAL and give it the value MinValue.

I don't know if that's best practice or not, but it works for us.

Eric
 
Eric Johannsen said:
We use DataType.MinValue to represent nulls.
We define a constant called something like
Constants.NULL_DECIMAL and give it the value
MinValue.
I don't know if that's best practice or not, but it
works for us.

Not best practice!
http://info.astrian.net/jargon/terms/h/hidden_flag.html

DataType.MinValue is a valid numeric value in itself, and you would
see some pretty strange behaviour in your program if anybody happened
to use it. SqlDecimal supports nulls and is designed specifically for
this purpose, so why not use it?

P.
 
Mark said:
We have a SQL Server table with a decimal column that is not required.
We're building a .NET application for data entry. If the value is optional,
but a decimal is strongly typed in C# and cannot be assigned a value of
null, what are the best practices for dealing with passing this potentially
null value around in a Decimal C# datatype?

Thanks in advance.

Mark

You will just love C#2.0 / CIL 2.0 as it sports nullable value types. They
are very useful.
As of now you are stuck using the bloated sql datatypes.

- Michael S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Access stripping decimal places in query 1
Round a decimal nullable 3
LINQ 1
Decimal numbers with possible null values? 3
decimal format 4
Take care of decimal gaps 5
Custom decimal round 14
A lot of Decimal Places 5

Back
Top