Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics 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 15th, 2004, 08:49 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default Dealing with DBNull

I'm currently building an object that receives values from a datareader - therefore my code looks a bit like this

Do while dr.Read
   With myobj
      .Prop1 = dr("Field1")
   End With
Loop

Now this obviously raises errors when we get null values, so I changed my code to the following;

Do while dr.Read
   With myobj
      .Prop1 = IIF(IsDBNull(dr("Field1")), "", dr("Field1"))
   End With
Loop

Whilst this works, it's not very elegant. I can create my own function where you passed in a value and it returns either the value or a zero length string. However, I wondered if there was a native vb.net command that would do something similar.

 
Old January 15th, 2004, 11:00 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I haven't found such a beast. One thing you can consider if you are using a SQL DB (not sure about Access): if you know what columns you might get db nulls on you can write the query to handle this. Instead of just selected the column value, use the COALESCE() function to get the first non-null value.

SELECT COALESCE(myColumn, '') ...

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 15th, 2004, 02:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Sparks, NV, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

Similar to the last post, in MS-SQL server you can use the ISNULL() funciton.

Lets say the nullable field is Price, and you want null values to be 0. You could use something like the following:

Code:
SELECT ISNULL(Price, 0) 'Price', ...
FROM ...
WHERE ...
This would return a field named Price. If the column value is not null, then it will return the value of the Price column. If the column value is null, then it would return 0.

You need the 'Price' to have it named, otherwise it would not keep the Price name.

John R Lick
JohnRLick@hotmail.com
 
Old January 16th, 2004, 10:59 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Thanks for the advice





Similar Threads
Thread Thread Starter Forum Replies Last Post
The DBNull Class and IsDBNull Function dschnedar BOOK: Professional Visual Basic 2008 ISBN: 978-0-470-19136-1 1 September 27th, 2008 09:13 AM
Convert DBNull to String GailCG ASP.NET 2.0 Basics 1 November 21st, 2006 02:16 AM
String="" convert to DBNull bekim ASP.NET 1.0 and 1.1 Basics 1 February 28th, 2006 01:28 AM
How to Check DBNull while binding controls narendra_patil General .NET 6 May 13th, 2005 07:20 AM
Comparing DBNull rajanikrishna General .NET 1 December 10th, 2004 11:13 AM





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