Solved

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

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

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?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

710 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