Solved

Handling Nulls in C#

Posted on 2003-10-21
6
1,221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

729 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