Solved

Handling Nulls in C#

Posted on 2003-10-21
6
1,205 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PAQed, with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now