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

Converting DBNull.Value

Hi

I wanto to know if there is a way to convert a DBNull.Value to Decimal and
returns to me Decimal.Zero?

Examples

COLUMN1 = NULL from DB

Ex1.: cstr((dt.rows(0).item("COLUMN1"))

returns Error

Ex2.: dt.rows(0).item("COLUMN1").toString

returns String.Empty

Ex3.: cdec((dt.rows(0).item("COLUMN1"))

returns Error

Is there a way to return to me Decimal.Zero??

I use this...

cdec((iif(dt.rows(0).item("COLUMN1") is dbnull.value,
decimal.zero,dt.rows(0).item("COLUMN1") ) )


Nov 20 '05 #1
3 18396
Your way works, the only other way I can think of is using IsNull(Column1,
0) in your SQL Statement, which may or may not be the way to go.

HTH,

Bill
"Joćo Roberto Alonso" <al******@kbonet.com.br> wrote in message
news:ud**************@tk2msftngp13.phx.gbl...
Hi

I wanto to know if there is a way to convert a DBNull.Value to Decimal and
returns to me Decimal.Zero?

Examples

COLUMN1 = NULL from DB

Ex1.: cstr((dt.rows(0).item("COLUMN1"))

returns Error

Ex2.: dt.rows(0).item("COLUMN1").toString

returns String.Empty

Ex3.: cdec((dt.rows(0).item("COLUMN1"))

returns Error

Is there a way to return to me Decimal.Zero??

I use this...

cdec((iif(dt.rows(0).item("COLUMN1") is dbnull.value,
decimal.zero,dt.rows(0).item("COLUMN1") ) )



Nov 20 '05 #2
"Joćo Roberto Alonso" <al******@kbonet.com.br> schrieb
Hi

I wanto to know if there is a way to convert a DBNull.Value to
Decimal and returns to me Decimal.Zero?

Examples

COLUMN1 = NULL from DB

Ex1.: cstr((dt.rows(0).item("COLUMN1"))

returns Error

Ex2.: dt.rows(0).item("COLUMN1").toString

returns String.Empty

Ex3.: cdec((dt.rows(0).item("COLUMN1"))

returns Error

Is there a way to return to me Decimal.Zero??

I use this...

cdec((iif(dt.rows(0).item("COLUMN1") is dbnull.value,
decimal.zero,dt.rows(0).item("COLUMN1") ) )

If you don't distinguish between Null and zero, why do you allow Null values
in the database at all?

Apart from your current working approach, the only shorter version I can
think of is a function performing the replacement if necessary:

Public Function DecimalNull2Zero(ByVal Value As Object) As Decimal
If TypeOf Value Is DBNull Then Return 0D
Return DirectCast(Value, Decimal)
End Function

Call:
dim d as decimal
d = DecimalNull2Zero(dt.rows(0).item("COLUMN1"))

--
Armin

Nov 20 '05 #3
i don't think there's an option or function in .net that will handle it for
you. you could create your own to handle various types with an option to
return a default value like this psuedo code:

function getDbValue(byval column as field, byval type as db.datatypes, byval
default as string) as object
on error resume next
if not isnull(column.value) return column.value
return ctype(default, type)
end function

*remember the above i *psuedo* code...b4 i get a million flames about the
details.
another route would be to overload the above function, removing the "type"
argument and setting the "default" to int16, int32, long, string, boolean,
etc. for each type of data you need to deal w/...i think the above would
save a lot of proc time when resolving which version vb should use and,
you'd be saving a ton of space in your code project.

the other option, if you have access and aproval, is to handle it w/n the
db.

queries, views, etc. should use:

sql server: isnull(myColumn, dbo.Zero)
oracle: nvl(myColumn, 0)

or in the table def, make the column "DEFAULT 0".

hth,

steve
"Joćo Roberto Alonso" <al******@kbonet.com.br> wrote in message
news:ud**************@tk2msftngp13.phx.gbl...
Hi

I wanto to know if there is a way to convert a DBNull.Value to Decimal and
returns to me Decimal.Zero?

Examples

COLUMN1 = NULL from DB

Ex1.: cstr((dt.rows(0).item("COLUMN1"))

returns Error

Ex2.: dt.rows(0).item("COLUMN1").toString

returns String.Empty

Ex3.: cdec((dt.rows(0).item("COLUMN1"))

returns Error

Is there a way to return to me Decimal.Zero??

I use this...

cdec((iif(dt.rows(0).item("COLUMN1") is dbnull.value,
decimal.zero,dt.rows(0).item("COLUMN1") ) )



Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Patrick.O.Ige | last post by:
When i try and use this (Where Unit is a column in my Table):- If Unit Is DBNull.Value Then Return "1" Else Return "2" End If I always have 2 returned! Even when Unit is NULL! I want a...
7
by: djc | last post by:
I have typically always used one of the VB CType functions (not CType itself but CInt, CString, etc...) to cast/convert an input value to its proper type before passing it as a parameter to a SQL...
4
by: Tina | last post by:
I have instantiated an insertRow for a dataset. I now want to make the GLCode DBNull. I have tried: insertRow.GLCode = Convert.dbnull insertRow.GLCode = Convert.dbnull(insertRow.GLCode) and...
4
by: Dursun | last post by:
Hi, I am trying to assign NULL to a datetime field in the SQL Server database. Here is the code that does NOT work: INSERT INTO ... .... VALUES ... .... CType(IIf(dateWitness2Date.Checked,...
6
by: tshad | last post by:
I have a value coming from my Object (or it could also be from a SqlDbReader) where I need to test for DBNull and 0. I tried to do it in one call: if (not (newPosition.ReportsTo is...
0
by: Elmo Watson | last post by:
I am having all kinds of problems with a Stored Proc I'm trying to run, with the error I'm getting above. First - the way I'm reading it - the database EXPECTS a TinyInt - and it thinks I'm...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
9
by: Robert Bravery | last post by:
HI all, I have a column value returned to a string variable in my c# app. But the return type is of system.dbnull. How can I convert that to a system.sting Thanks Robert
1
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.