I have a DataTable table. from that table I read value from a column, which is typeof(decimal). I try to cast this value to decimal? dec. When value is null, I get an exception. I tried it this way:
decimal? dec= row["myRow"] == null ? null : (decimal?)row["myRow"];
//this thorws "Specified cast is not valid." Exception
decimal? dec= myRdr["myValue"] == DBNull.Value ? null : (decimal?)myRdr["myValue"];
//this does NOT throw an Exception??
I tried the second solution when reading from SQL data table using SQLDataReader and it worked with no problems.