Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 15th, 2004, 07: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.

Reply With Quote
  #2 (permalink)  
Old January 15th, 2004, 10: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.
Reply With Quote
  #3 (permalink)  
Old January 15th, 2004, 01: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
Reply With Quote
  #4 (permalink)  
Old January 16th, 2004, 09: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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 01:16 AM
String="" convert to DBNull bekim ASP.NET 1.0 and 1.1 Basics 1 February 28th, 2006 12: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 10:13 AM



All times are GMT -4. The time now is 03:18 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.