Solved

Handling Nulls in C#

Posted on 2003-10-21
6
1,216 Views
Last Modified: 2012-06-21
I am pulling values from a database using code like this to transfer it to my local object:

prospect.Loans = Convert.ToDecimal(result["Loans"]);
prospect.Liabilities = Convert.ToDecimal(result["Liabilities"]);
prospect.Bankrupt = Convert.ToBoolean(result["Bankrupt"]);
prospect.BankruptDesc = result["BankruptDesc"].ToString();
prospect.Dependents = Convert.ToInt32(result["Dependents"]);

When the database has a Null value, this bombs out. What can I use to trap Null values and convert them to zeros or blanks? The VS.Net Help file only gave me the answer for Visual FoxPro (@*#$@#) (How dumb is THAT?).

I need some help right away on this.

Thanks, Ted
0
Comment
Question by:tk3
6 Comments
 

Expert Comment

by:ianshave
ID: 9599778
Hello Ted

The only way i can think of at the mo, is to check each field individually to see if it contains a null value and if it does then assign the attribute a 0 else give it the value from the database!

if(results["Loans"].ToString() != "")
       prospect.Loans = Convert.ToDecimal(result["Loans"]);
else
      prospect.Loans = 0;

Hope it helps

Ian


     
0
 

Author Comment

by:tk3
ID: 9599850
Ian,
   I think the above doesn't work in the case of Nulls. I found some code which works using the DBNull object.

if (result["AddressId"] == DBNull.Value)
     prospect.AddressId = 0;
else
     prospect.AddressId = Convert.ToInt32(result["AddressId"]);

   I had used something similar in VB, but had a hard time tracking down the C# equivalent.
   Thanks for trying....
   Ted
0
 

Expert Comment

by:ianshave
ID: 9599985
Ted

It definitely does work, I’ve got it in my current project! Otherwise;

if(results["Loans"].ToString() != "" && results["Loans"].ToString() != null )

Would work too......but glad your sorted!

Ian


0
 

Expert Comment

by:kaanoezturk
ID: 9770127
Hi Ted, Ian,

I had the problem too.
The code like the one from Ian did not work.
I had to use:
if(!Convert.IsDBNull(reader["Loans"])
   ...

KaaN
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11687312
PAQed, with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question