Solved

Handle null currency value from dataset c#

Posted on 2011-09-20
5
362 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
[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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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