Is there a better/cleaner way to do this?
int stockvalue = 0;
if (!Convert.IsDBNull(reader["StockValue"]))
stockvalue = (int)reader["StockValue"];
question from:https://stackoverflow.com/questions/2433155/handle-dbnull-in-c-sharpIs there a better/cleaner way to do this?
int stockvalue = 0;
if (!Convert.IsDBNull(reader["StockValue"]))
stockvalue = (int)reader["StockValue"];
question from:https://stackoverflow.com/questions/2433155/handle-dbnull-in-c-sharpThe shortest (IMHO) is:
int stockvalue = (reader["StockValue"] as int?) ?? 0;
Explanation: