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

Problem with DBNULL and Integers

This is my code. If the value coming in from the database table is null, I
wish to place a 0 in the column or otherwise the value itself. Everything I
try this code I get an error. Am I doing this wrong that a null value cannot
be converted to any other type?

If Not Row("MonthsPaid") Is DBNull.Value Then
paid.MonthsPaid = 0
Else
paid.MonthsPaid = Convert.ToInt32(Row("MonthsPaid"))
End If

Jul 21 '05 #1
2 2975
Hi Bill,

VB isn't my strong side, but shouldn't your condition be swapped?

If Row("MonthsPaid") Is DBNull.Value
On Fri, 06 May 2005 17:50:05 +0200, BillG <Bi***@discussions.microsoft.com> wrote:
This is my code. If the value coming in from the database table is null, I
wish to place a 0 in the column or otherwise the value itself. Everything I
try this code I get an error. Am I doing this wrong that a null value cannot
be converted to any other type?

If Not Row("MonthsPaid") Is DBNull.Value Then
paid.MonthsPaid = 0
Else
paid.MonthsPaid = Convert.ToInt32(Row("MonthsPaid"))
End If


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2
Well here's one way to do it. Add this function to your class:

Public Function NullToZero(ByVal oValue As Object) As Double
If IsNothing(oValue) OrElse Convert.IsDBNull(oValue) OrElse Not
IsNumeric(oValue) Then
NullToZero = 0
Else
NullToZero = Convert.ToDouble(oValue)
End If
End Function

Now you can reduce your code-snippet below to just one line:

paid.MonthsPaid = NullToZero(Row("MonthsPaid"))
Kevin

"BillG" <Bi***@discussions.microsoft.com> wrote in message
news:10**********************************@microsof t.com...
This is my code. If the value coming in from the database table is null,
I
wish to place a 0 in the column or otherwise the value itself. Everything
I
try this code I get an error. Am I doing this wrong that a null value
cannot
be converted to any other type?

If Not Row("MonthsPaid") Is DBNull.Value Then
paid.MonthsPaid = 0
Else
paid.MonthsPaid = Convert.ToInt32(Row("MonthsPaid"))
End If

Jul 21 '05 #3

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

Similar topics

9
by: Miky | last post by:
I have the following problem: I am populating a dataset but, sometimes (NOT always), the NewRow method works like the Add method. SetRightDetail.RightDetail.Rows.Clear() For Each Right In...
6
by: Ron L | last post by:
I have a dataset whose source is a SQL 2k stored procedure that I am trying to display in a datagrid. This datasource has 4 columns that I am interested in here, a text column and 3 value columns...
5
by: Chad | last post by:
I want to create a class which contains a date, integer and string property which all allow a NULL value. I don't want to store the values in an Object datatype. I prefer more strongly typed...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
17
by: John | last post by:
Hi I have a datadapter with the following SQL; SELECT ID, Company, Status, CompanyType FROM Companies WHERE (@Status IS NULL or @Status = Status) When I try to fill like...
2
by: BillG | last post by:
This is my code. If the value coming in from the database table is null, I wish to place a 0 in the column or otherwise the value itself. Everything I try this code I get an error. Am I doing...
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...
8
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
7
by: David | last post by:
Hi, using C# asp.net 1.1 I am having a problem. If I use the code below, but have null passed as the PostingGuid, then by the time I get to fill the dataset, I have "an object is not set to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.