Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 31st, 2006, 10:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default Handling Null Values

Whats the best way to handle Null values stored in my database? I keep getting errors when I retrieve Null values from the database and try to bind or work with them in code, so how best should I deal with these...? I have a few ideas on how to cut down on this...

-Don't store Null values in the first place. I am using SQL server and I set all default values to 0 for numbers or '' for strings. I dpn't hknow how to deal with dates however so I leave those Null.

-Using a check to System.DBNull...either in code or as a set of common Functions. I currently do not employ this much. I have seen several versions of this though:

This which gives error:
Code:
If TypeOf objUser.DOB Is System.DBNull Then
        'No Date

    Else
        'Return the Date

    End If
Error: Type Of...Is' requires its left operand to have a reference type, but this operand has the value type 'Date'(???)

and I came up with this, which might work:
Code:
If CType(objUser.DOB,Object) Is System.DBNull.Value Then
        'No Date

    Else
        'Return the Date

    End If


-Lastly, and I use this a LOT, is to use IsNull in my SQL. This is a horror to write and maintain. Instead of having:

Code:
SELECT * FROM tableUsers
I must use:
Code:
SELECT IsNull(UserName,'') As UserName, IsNull(FirstName,'') As FirstName, IsNull(LastName,'') As LastName, IsNull(DOB,'') As DOB etc. etc.
Any suggestions are appreciated!


-------------------------
Beware of programmers with screwdrivers...
__________________
-------------------------
Beware of programmers with screwdrivers...
 
Old January 31st, 2006, 02:02 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can check against DBNull.Value, like this:

if (myDataReader(0) != DBNull.Value)
{
  string test = myDataReader.GetString(0);
}

So, you were very close...

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Null values jmcgranahan BOOK: Access 2003 VBA Programmer's Reference 0 August 29th, 2006 02:25 PM
Passing null values maddy137 ASP.NET 1.0 and 1.1 Basics 1 May 7th, 2006 12:26 PM
Handling Null Values nvenkat75 ADO.NET 2 January 16th, 2006 05:46 PM
Catching NULL values hasanali00 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 July 1st, 2005 10:29 PM
Handling Null Dates [email protected] ASP.NET 1.0 and 1.1 Professional 12 February 27th, 2004 07:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.