473,657 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Pattern for Handling Null values in .NET objects

Hi,

I am looking for opinions and alternatives for handling
null values in a data object which reads a record from a
database table.

This object will have properties which will be populated
from the DB table columns, standard enough. But what
about the instance where a column could be null.

The strategy I have used before is to assign a known
value to a property where the DB column is null
(Integer.Minval ue for a property of type integer).
The object then knows that the property is really null
when the DB is being updated from the object,

Has anyone any better pattern for doing this, or
alternate design strategies,

Brendan
Nov 22 '05 #1
8 2207
If you are using stored procedures you can use the isnull
() MSSql method.

isnull(name,"") as kundenr,
-----Original Message-----
Hi,

I am looking for opinions and alternatives for handling
null values in a data object which reads a record from a
database table.

This object will have properties which will be populated
from the DB table columns, standard enough. But what
about the instance where a column could be null.

The strategy I have used before is to assign a known
value to a property where the DB column is null
(Integer.Minva lue for a property of type integer).
The object then knows that the property is really null
when the DB is being updated from the object,

Has anyone any better pattern for doing this, or
alternate design strategies,

Brendan
.

Nov 22 '05 #2
If you are using stored procedures you can use the isnull
() MSSql method.

isnull(name,"") as kundenr,
-----Original Message-----
Hi,

I am looking for opinions and alternatives for handling
null values in a data object which reads a record from a
database table.

This object will have properties which will be populated
from the DB table columns, standard enough. But what
about the instance where a column could be null.

The strategy I have used before is to assign a known
value to a property where the DB column is null
(Integer.Minva lue for a property of type integer).
The object then knows that the property is really null
when the DB is being updated from the object,

Has anyone any better pattern for doing this, or
alternate design strategies,

Brendan
.

Nov 22 '05 #3
So you would essentialy replace the null value with a
default value like integer properties are assigned 0 or
Integer.minvalu e and when you are going to the database
convert this back to null when adding it to the stored
procedure param list.

B.
-----Original Message-----
If you are using stored procedures you can use the isnull
() MSSql method.

isnull(name,"" ) as kundenr,
-----Original Message-----
Hi,

I am looking for opinions and alternatives for handling
null values in a data object which reads a record from adatabase table.

This object will have properties which will be populatedfrom the DB table columns, standard enough. But what
about the instance where a column could be null.

The strategy I have used before is to assign a known
value to a property where the DB column is null
(Integer.Minv alue for a property of type integer).
The object then knows that the property is really null
when the DB is being updated from the object,

Has anyone any better pattern for doing this, or
alternate design strategies,

Brendan
.

.

Nov 22 '05 #4
So you would essentialy replace the null value with a
default value like integer properties are assigned 0 or
Integer.minvalu e and when you are going to the database
convert this back to null when adding it to the stored
procedure param list.

B.
-----Original Message-----
If you are using stored procedures you can use the isnull
() MSSql method.

isnull(name,"" ) as kundenr,
-----Original Message-----
Hi,

I am looking for opinions and alternatives for handling
null values in a data object which reads a record from adatabase table.

This object will have properties which will be populatedfrom the DB table columns, standard enough. But what
about the instance where a column could be null.

The strategy I have used before is to assign a known
value to a property where the DB column is null
(Integer.Minv alue for a property of type integer).
The object then knows that the property is really null
when the DB is being updated from the object,

Has anyone any better pattern for doing this, or
alternate design strategies,

Brendan
.

.

Nov 22 '05 #5
Try this

When setting the object properties

If IsDBNull(Fields (i)) The
obj.Properties( i)=Nothin
Els
obj.Properties( i)=Fields(i
End I

When saving the changes to the DB
If obj.Properties( i)=Nothing The
Fields(i)=Syste m.DBNull.Valu
Els
Fields(i)=obj.P roperties(i
End I

Hope it works for you.
Nov 22 '05 #6
Try this

When setting the object properties

If IsDBNull(Fields (i)) The
obj.Properties( i)=Nothin
Els
obj.Properties( i)=Fields(i
End I

When saving the changes to the DB
If obj.Properties( i)=Nothing The
Fields(i)=Syste m.DBNull.Valu
Els
Fields(i)=obj.P roperties(i
End I

Hope it works for you.
Nov 22 '05 #7
I have always tended to use 2 properties for this and populate private
variables as well. This works then for databinding also. When accessing the
value, simply test the IsNull property before retrieving the actual.

private int foreignKey = 0;
private bool foreignKeyIsNul l = false;

public int ForeignKey {
get {return this.foreignKey ;}
set {this.foreignKe y = value;}
}

public bool ForeignKeyIsNul l {
get {return this.foreignKey IsNull;}
set {this.foreignKe yIsNull = value;}
}

private void Scatter(dataRow row) {
if (!(foreignKeyIs Null = (dataRow["ForeignKey "].Value == DBNull.Value)))
this.foreignKey = (int)dataRow[ForeignKey"].Value;
}

private void Gather(dataRow row) {
if (this.foreignKe yIsNull)
dataRow["ForeignKey "].Value = DBNull.Value;
else
dataRow["ForeignKey "].Value = this.foreignKey ;
}

"Brendan McLoughlin" <br************ ****@gmaccm.com > wrote in message
news:12******** *************** ******@phx.gbl. ..
Hi,

I am looking for opinions and alternatives for handling
null values in a data object which reads a record from a
database table.

This object will have properties which will be populated
from the DB table columns, standard enough. But what
about the instance where a column could be null.

The strategy I have used before is to assign a known
value to a property where the DB column is null
(Integer.Minval ue for a property of type integer).
The object then knows that the property is really null
when the DB is being updated from the object,

Has anyone any better pattern for doing this, or
alternate design strategies,

Brendan

Nov 22 '05 #8
I have always tended to use 2 properties for this and populate private
variables as well. This works then for databinding also. When accessing the
value, simply test the IsNull property before retrieving the actual.

private int foreignKey = 0;
private bool foreignKeyIsNul l = false;

public int ForeignKey {
get {return this.foreignKey ;}
set {this.foreignKe y = value;}
}

public bool ForeignKeyIsNul l {
get {return this.foreignKey IsNull;}
set {this.foreignKe yIsNull = value;}
}

private void Scatter(dataRow row) {
if (!(foreignKeyIs Null = (dataRow["ForeignKey "].Value == DBNull.Value)))
this.foreignKey = (int)dataRow[ForeignKey"].Value;
}

private void Gather(dataRow row) {
if (this.foreignKe yIsNull)
dataRow["ForeignKey "].Value = DBNull.Value;
else
dataRow["ForeignKey "].Value = this.foreignKey ;
}

"Brendan McLoughlin" <br************ ****@gmaccm.com > wrote in message
news:12******** *************** ******@phx.gbl. ..
Hi,

I am looking for opinions and alternatives for handling
null values in a data object which reads a record from a
database table.

This object will have properties which will be populated
from the DB table columns, standard enough. But what
about the instance where a column could be null.

The strategy I have used before is to assign a known
value to a property where the DB column is null
(Integer.Minval ue for a property of type integer).
The object then knows that the property is really null
when the DB is being updated from the object,

Has anyone any better pattern for doing this, or
alternate design strategies,

Brendan

Nov 22 '05 #9

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

Similar topics

4
332
by: Brendan McLoughlin | last post by:
Hi, I am looking for opinions and alternatives for handling null values in a data object which reads a record from a database table. This object will have properties which will be populated from the DB table columns, standard enough. But what about the instance where a column could be null.
2
5119
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data store. I've done a fair amount of research on concurrency handling in newsgroups and other resources. Below is what I've come up as a standard for handling concurrency thru stored procedures. I am sharing with everyone so I can get some comments...
7
1837
by: farseer | last post by:
Here is the scenario: I have an interface which defines get methods for data that will make up a row in a table. However, the source of this data may, over time, switch/change (The company may choose to change data providers). Therefore i thought to myself, a type of Adapter Pattern is best here and so i proceeded with that. here's an example of what i did (note this implementation differs from the text book one due to the way data...
9
6255
by: Hasan O. Zavalsiz | last post by:
Hi , i am trying to figure out which approach is better to use . let me explain the scenario. i am using the "Nortwind" database . in this database i have "Customers " table .The following is the two different ways to handle this table. CASE 1 : create a struct that encaplusates table "Customers" columns public struct structCustomers { public string CustomerID;
8
2816
by: Gaensh | last post by:
HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance; /// <summary> /// public property that can only get the single instance of this class. /// </summary>
5
1736
by: Eric | last post by:
I am implementing a variation on the Singleton design pattern, that allows up to 8 objects of a class to be instantiated, and returns a null pointer for anything more than 8. I am running into a compile problem with GNU g++. Here is the code: /******************** FILE sample.h ********************/
34
11177
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
4
1661
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: http://mail.python.org/pipermail/python-dev/2007-February/070921.html http://mail.python.org/pipermail/python-dev/2007-February/071155.html http://mail.python.org/pipermail/python-dev/2007-February/071181.html I'd love to have feedback on this PEP: - from a user's perspective (would you want to write...
4
2109
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a prediction is ready, then there is a "WeatherViewer" which calls methods from the "Observer Interface".
10
1225
by: richard.markiewicz | last post by:
Hi Gurus, Working with a .NET 2 C# web application. Using System.DirectoryServices a lot to read information from AD. There seemed to be several bugs in the .NET 1.1 implementation of that DLL and out of habit I now very aggressively dispose of objects when I'm done with them. Is there anything wrong with this pattern:
0
8316
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
8737
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...
0
8610
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
7345
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...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
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
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
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
2
1967
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.