Solved

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

Posted on 2014-03-24
2
431 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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39951105
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
ID: 39951221
Works as well as anything I've found so far... Thanks.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

828 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