Solved

Is there a way to handle a DataColumn returning DBNull in a single line statement?

Posted on 2014-03-24
2
423 Views
Last Modified: 2014-03-24
I have a table returning a column defined at decimal(18,2).  This column is allowed to return a DBNull.  I am trying to eloquently handle the value in this column as I am using it just once in a method as part of an equation.  This scenario is repeated for about 15 more columns returned in this table.  Each column is capable of returning as DBNull and I need to be able to use (or convert for use) the DBNull as a decimal.

decimal.Parse(ds.Tables[0].Rows[0]["MyNullableColumn"].ToString())  will throw an exception when the data in the column is null.

Thanks,
Bruce
0
Comment
Question by:Bruce
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
I suspect what you are after is the ternary operator:

ds.Tables[0].Rows[0]["MyNullableColumn"] == DBNull.Value ? 0M : Convert.ToDecimal(ds.Tables[0].Rows[0]["MyNullableColumn"])

Open in new window

0
 
LVL 1

Author Closing Comment

by:Bruce
Comment Utility
Works as well as anything I've found so far... Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 41
Need Counts 11 40
Long URL in SMS 6 22
Sql query 34 16
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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