Solved

Handle null currency value from dataset c#

Posted on 2011-09-20
5
358 Views
Last Modified: 2012-06-27
I have a dataset returning a series of currency values which I am then populating into label controls. It is possible that null values could be returned in the dataset for some or all of these, at which point my conversion/formatting methods below fail and throw an error.

I'm looking for a clean efficient way  to handle these nulls.

Thanks.
lblEEonlyDedMax.Text = Convert.ToDecimal(ds.Tables[0].Rows[0]["new_hraeeonlydedmax"]).ToString("C2");

Open in new window

0
Comment
Question by:sailing_12
  • 3
  • 2
5 Comments
 
LVL 40
ID: 36567825
if (! DBNull.Value.Equals(ds.Tables[0].Rows[0]["new_hraeeonlydedmax"]))) 
        lblEEonlyDedMax.Text = Convert.ToDecimal(ds.Tables[0].Rows[0]["new_hraeeonlydedmax"]).ToString("C2");
     else
        lblEEonlyDedMax.Text = "" // Or whatever you want to display when null;

Open in new window

0
 

Author Comment

by:sailing_12
ID: 36567979
Yeah, I thought of that but really didn't want to have to wrap them all in if tests since there are about 20 of them. I was hoping for a more streamlined solution.
0
 
LVL 40
ID: 36568378
You might not have control over this, but the best solution is not to permit Null in the fields.

I usually do not permit nulls, and use a default value that i know could not exist in a real situation. In the case of numbers, I often consider 0 to be my Null and code around that. Usually, in most instances, if a currency value is Null, a value of 0 does the job very well.

Just as you would for Null, you can check for that value on data entry forms to prevent the user from omitting a value.

But when you read them back from the database, nothing special to do. They simply display 0.
0
 

Accepted Solution

by:
sailing_12 earned 0 total points
ID: 36569025
This works.
lblEEonlyDedMax.Text = String.Format("{0:C}", ds.Tables[0].Rows[0]["new_hraeeonlydedmax"]);

Open in new window

0
 

Author Closing Comment

by:sailing_12
ID: 36594709
This works with minimal code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

22 Experts available now in Live!

Get 1:1 Help Now