473,320 Members | 1,939 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,320 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 2170
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.