[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Handling Nulls in C#

Posted on 2003-10-21
6
Medium Priority
?
1,232 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
5 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

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 …
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…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
Suggested Courses

873 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