473,763 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10735
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****@idonotl ikespam.cce.umn .edu> wrote in message
news:e1******** ******@TK2MSFTN GP10.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.MinVal ue 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******** ******@TK2MSFTN GP10.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****@idonotl ikespam.cce.umn .edu> wrote in message
news:e1******** ******@TK2MSFTN GP10.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*********@jo hannsen.us> wrote:
We use DataType.MinVal ue 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.MinVal ue 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****@idonotl ikespam.cce.umn .edu> wrote in message
news:e1******** ******@TK2MSFTN GP10.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
1900
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. The " & "" " on the right of each line should prevent nulls from being
2
2364
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 them in the columns anymore? If so, how to go about removing the nulls with a script?
3
2014
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 certain tasks have been completed. A typical example would be a column say invoice_value
6
28301
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 I'm no SQL expert, but if I'm summing (SUM) multiple fields and adding them together in my SELECT how does SUM handle Null? In some situations a single column in a single row is Null but and it's part of a larger GROUP BY and SUM and from looking...
0
1585
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, called Half, has a single integer input parameter and divides this value by two to produce its result. It also tests the input parameter to see if it is null; if it is null, it returns null. When I click the Run button and try to use the SET TO...
1
2073
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 boat name in a text field OR a sail number in a text field, and gets the desired boat record back (an exact match).
13
2381
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 and so forth to the 1st string. Any ideas how to go about this? Thanks,
1
2067
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 place, but displays "No Max", but if it is not null, then it would display the decimal value in place. How can I do this in the gridview.
6
5454
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 it--then hold the next value through the next set of nulls, and so on. See example below: I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10. Then copy down batch "7138" all the way to ID 20 and so on....
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.