473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

decimal - nulls

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
Nov 16 '05 #1
4 10711
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.

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2
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
"Marina" <so*****@nospam.com> wrote in message
news:uv**************@TK2MSFTNGP10.phx.gbl...
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.

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
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



Nov 16 '05 #3
"Eric Johannsen" <no*********@johannsen.us> wrote:
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.
Nov 16 '05 #4

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
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
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan Perlman | last post by:
From: "Dan Perlman" <dan@dpci.NOSPAM.us> Subject: ODBC creating nulls? Date: Friday, July 09, 2004 10:43 AM Hi, Below is my VB6 code that writes data from an Access 2000 table to a PG table....
2
by: Steve Walker | last post by:
Hi all. I've been tasked with "speeding up" a mid-sized production system. It is riddled with nulls... "IsNull" all over the procs, etc. Is it worth it to get rid of the nulls and not allow...
3
by: aaj | last post by:
Hi I am probably going to regret asking this because I'm sure you are going to tell me my design is bad 8-) ah well we all have to learn.... anyway I often use Nulls as a marker to see if...
6
by: mike | last post by:
I'm doing what I thought was a simple GROUP BY summary of fairly simple data and the my numbers aren't working out Some results are showing up <NULL> when I know the data is in the database ...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
13
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string...
1
by: Microsoft News Group | last post by:
I am trying to make a column which is decimal format, convert nulls to a certain string such as "No Max". This is for a price range, and if they do not put a value in, a null value is put in...
6
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.