Solved

Handle null currency value from dataset c#

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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