Wednesday, June 22, 2011

Storing null in a Column will compile, but will cause run time exception

You can legally store a null in a column from a data row and the code will compile. Don't do this thought because it will definitely throw an exception during run time :)

dr["columnName"] = null; //This will throw an exception during run time

The remedy is to just use DBNull.Value:

dr["columnName"] = DBNull.Value; //This is legal

No comments:

Post a Comment