Solved

Handling Nulls in C#

Posted on 2003-10-21
6
1,213 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
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…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

777 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