Solved

Handle null currency value from dataset c#

Posted on 2011-09-20
5
357 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now