Solved

Handle null currency value from dataset c#

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 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