john doe
john doe

Reputation: 9662

Inline Check for IsDbNull in C#

How can I write the following line:

 document.Total = reader.IsDBNull(reader.GetOrdinal("Total")) == true : 0.0m ? (decimal?)reader["Total"];

The error I am getting is that cannot convert type from bool to decimal?

Upvotes: 0

Views: 377

Answers (2)

Juan M. Elosegui
Juan M. Elosegui

Reputation: 6981

Just to add a shorter way to handle DBNull.Value and DataReaders.

Consider using

document.Total = (reader["Total"] as decimal?) ?? 0.0m;

Upvotes: 1

sstan
sstan

Reputation: 36513

You accidentally flipped the ? and :. It should be:

document.Total = reader.IsDBNull(reader.GetOrdinal("Total")) == true ? 0.0m : (decimal?)reader["Total"];

Upvotes: 4

Related Questions