Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-03-24
2
Medium Priority
?
437 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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

972 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