473,750 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NULL values

How are people handling NULL values for value types.

Descriptions of problem

Say you have a typical method with the signature:

public int InsertPerson(st ring firstName, string lastName, int
personCategoryI D)

Now say you only want the firstName and lastName parameters to be
required and to retain the NULL value in the database for
personCategoryI D. Here are a couple typical solutions for this
scenerio would be:

A. Overload the InsertPerson method and create the following method.

public int InsertPerson(st ring firstName, string lastName)

So when you detect in your UI that personCategoryI D is to be NULL or
"empty" you will call InsertPerson with two parameters. Therefore,
the personCategoryI D field in the database will remain NULL.

B. Treat the int as an object and use boxing and unboxing. In my
example convert the int parameter to an object type and check for null
in the method so I can set the stored procedure parameter to DBNull.

My slant on the problem

I happen to have inserts for different types of "People" that have
many (30-40) parameters in their insert methods. So option A above
would not be feasable and option B is more likely however I would have
to modify alot of code to change all my int it objects and use boxing
and unboxing(but certainly feasible). I am interested to hear how
other people are handling this problem.

JF
Nov 15 '05 #1
4 16197
James Fisher wrote:
public int InsertPerson(st ring firstName, string lastName)


Instead of overloading, you could change the signature to accept an
object.

Personally, I prefer to manipulate the data directly within a DataSet.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #2
Hi all
From my understanding of the framework etc. everything is inherited from an
object even system types such as int.

You can declare a variable as an int with a value of null
eg int personCategoryI D = null;

The framework will then give it the default value of null for an int which
is 0.

You can then test for this in your function - e.g.
public int InsertPerson(st ring firstName, string lastName, int
personCategoryI D)
{
if (personCategory ID != null)
{
//code
}
}

The only downside to this is if you use the default value (i.e. 0 in this
case) in your database, but thats just a management thing - just start from
1.

Of course the other aregument about whether you should use integers or GUIDs
for IDs comes to mind to. I would use the GUID and test for null (again a
string of 0's in the GUID format)

Hope it helps
Mark
"James Fisher" <jf*****@snip.n et> wrote in message
news:76******** *************** ***@posting.goo gle.com...
How are people handling NULL values for value types.

Descriptions of problem

Say you have a typical method with the signature:

public int InsertPerson(st ring firstName, string lastName, int
personCategoryI D)

Now say you only want the firstName and lastName parameters to be
required and to retain the NULL value in the database for
personCategoryI D. Here are a couple typical solutions for this
scenerio would be:

A. Overload the InsertPerson method and create the following method.

public int InsertPerson(st ring firstName, string lastName)

So when you detect in your UI that personCategoryI D is to be NULL or
"empty" you will call InsertPerson with two parameters. Therefore,
the personCategoryI D field in the database will remain NULL.

B. Treat the int as an object and use boxing and unboxing. In my
example convert the int parameter to an object type and check for null
in the method so I can set the stored procedure parameter to DBNull.

My slant on the problem

I happen to have inserts for different types of "People" that have
many (30-40) parameters in their insert methods. So option A above
would not be feasable and option B is more likely however I would have
to modify alot of code to change all my int it objects and use boxing
and unboxing(but certainly feasible). I am interested to hear how
other people are handling this problem.

JF

Nov 15 '05 #3
su******@bellso uth.net (bjs10) wrote in message news:<58******* *************** ****@posting.go ogle.com>...
This won't work; you can't set a value type to null.

"Mark" <f1*****@hotmai l.com> wrote in message news:<#d******* ******@TK2MSFTN GP10.phx.gbl>.. .
Hi all
From my understanding of the framework etc. everything is inherited from an
object even system types such as int.

You can declare a variable as an int with a value of null
eg int personCategoryI D = null;

The framework will then give it the default value of null for an int which
is 0.


Yes bjs is correct,

There really is no great solution to this. If I were using VB.NET I
could use the combination of Nothing and IsNull and not have to change
much. However, using C# I created a wrapper type for my value types.
For example, I created an Integer class that wraps the good ole' int.
It has two properties which are IsNull and Value. So by changing my
object properties to Integers lets me check whether or not they are
null before using them and properly inserting a NULL into the
database.

JF
Nov 15 '05 #4
su******@bellso uth.net (bjs10) wrote in message news:<58******* *************** ****@posting.go ogle.com>...
This won't work; you can't set a value type to null.

"Mark" <f1*****@hotmai l.com> wrote in message news:<#d******* ******@TK2MSFTN GP10.phx.gbl>.. .
Hi all
From my understanding of the framework etc. everything is inherited from an
object even system types such as int.

You can declare a variable as an int with a value of null
eg int personCategoryI D = null;

The framework will then give it the default value of null for an int which
is 0.


Yes bjs is correct,

There really is no great solution to this. If I were using VB.NET I
could use the combination of Nothing and IsNull and not have to change
much. However, using C# I created a wrapper type for my value types.
For example, I created an Integer class that wraps the good ole' int.
It has two properties which are IsNull and Value. So by changing my
object properties to Integers lets me check whether or not they are
null before using them and properly inserting a NULL into the
database.

JF
Nov 15 '05 #5

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

Similar topics

1
3052
by: Marcus | last post by:
Hello, quick question about MySQL storing NULL values... Say I have a textbox called $_POST and a variable $var. if(empty($_POST)) $var = NULL; else $var = $_POST; Disregarding filtering/formatting the data, upon inserting $var into
26
45443
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
4
2025
by: Ellen Manning | last post by:
Using SQL2000. I want to return the # of columns with non-null values. Here's my query so far: select case when Dx1 is not null then 0 else 1 end + case when Dx2 is not null then 0 else 1 end + case when Dx3 is not null then 0 else 1 end + case when Dx4 is not null then 0 else 1 end as DxCount from tblClerkshipDataClean where PalmName = @PalmName
3
2128
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in Perl, and Nulls have started to bother me greatly. The first issue is, as far as I understand it, a column should be NOT NULL if it is necessary (required) data. Now, if a column doesn't have to be NOT NULL; that is, it's not _required_ data,...
8
12549
by: manning_news | last post by:
Using SQL2000. According to Books Online, the avg aggregrate function ignores null values. ((3+3+3+3+Null)/5) predictably returns Null. Is there a function to ignore the Null entry, adjust the divisor, and return a value of 3? For example:((3+3+3+3)/4) after ignoring Null entry. If there's more than one null value, then adjust divisor accordingly. For example: ((5+5+5+4+Null+5+5+Null)/8) would be ((5+5+5+4+5+5)/6) after nulls...
13
8714
by: Federico Balbi | last post by:
Hi, I was wondering if PGSQL has a function similar to binary_checksum() of MS SQL Server 2000. It is pretty handy when it comes to compare rows of data instead of having to write long boolean expressions. binary_checksum() takes a list of fields and it returns an integer value which sumarize the row content. Thanks, Fed
17
4534
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are selected into 2 different SP variables and compared for equal. They are both NULL, but do not compare as equal. When the Not NULL columns (SALARY) are compared, they do compare as equal.
15
28401
by: info | last post by:
Hi, I need this behaviour: 1 + null = 1 I have a (dynamic) set of many columns containing decimals that I want to add as follows: if all columns are null the result should be null if not all columns are null, the null columns may be regarded as 0. E.g. null + null + 1 = 1
3
12304
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero (0) length string, an empty Field, or no value at all - so exactly what is Null? The purpose of this Topic is hopefully to explain what a Null Value is, discuss some peculiarities about Nulls, show how we can detect them, and finally, how to convert...
10
9037
by: Toby Gallier | last post by:
Hello! I have a form that is calculating averages as follows: " =(NZ()+Nz()+Nz())/3 " However I need to now adjust for null values , so for example if value2 is null I would then need to base my average on just 2 values instead of 3 i am currently using in my string. How can i have the form update the "3" based on the number of values that are populated?
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8836
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
9575
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
9394
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
9338
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
9256
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...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.