As said in comments, DBNull.Value
and null
are not same thing. So when you are writing:
decimal? dec= row["myRow"] == null ? null : (decimal?)row["myRow"];
you are trying doing this:
if(DBNull.Value == null)
{
return null;
}
else
{
return (decimal?)row["myRow"];
}
which is false so the second part (in else)
(decimal?)row["myRow"];
is executed. This throws an exception.
But in second case:
if(DBNull.Value == DBNull.Value)
{
return null;
}
else
{
return (decimal?)row["myRow"];
}
is true so the cast statement is not executed and null is returned.