Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
435 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
[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
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

670 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