I\'m trying to add a column to an existing DataRow in C#. Afterwards the column will be filled with a single value from my database.
DataRow dr already
Use the ISNULL() function in your SQL.
ISNULL ( check_expression , replacement_value )
ie...
SELECT ISNULL(SUM(Price),0) FROM Order
ExecuteScalar returns DBNull for null value from query and null for no result. Maybe on your development server it never occured (null result from query).
Clearly in production you have either a NULL returned from the command execution or something different in the connectionstring or whatever; as a general rule you should always test for DBNull before casting/converting directly to another type the result of ExecuteScalar.
Check Rein's answer here (and vote him up) for his nice suggested solution:
Unable to cast object of type 'System.DBNull' to type 'System.String`