473,385 Members | 1,863 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,385 software developers and data experts.

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.Minvalue 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 2173
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.Minvalue 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.Minvalue 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.minvalue 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.Minvalue 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.minvalue 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.Minvalue 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)=System.DBNull.Valu
Els
Fields(i)=obj.Properties(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)=System.DBNull.Valu
Els
Fields(i)=obj.Properties(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 foreignKeyIsNull = false;

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

public bool ForeignKeyIsNull {
get {return this.foreignKeyIsNull;}
set {this.foreignKeyIsNull = value;}
}

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

private void Gather(dataRow row) {
if (this.foreignKeyIsNull)
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.Minvalue 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 foreignKeyIsNull = false;

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

public bool ForeignKeyIsNull {
get {return this.foreignKeyIsNull;}
set {this.foreignKeyIsNull = value;}
}

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

private void Gather(dataRow row) {
if (this.foreignKeyIsNull)
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.Minvalue 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
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...
2
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...
7
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...
9
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...
8
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;...
5
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...
34
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...
4
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: ...
4
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...
10
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.