Handling System.DBNull graciously

Posted by Sitt Sen Chok | Filed under

Data fetched from database may contain null values.  This can happen either by design or the enforcing constrain unintentionally missed in the database schema.  The latter case calls for special care at the data access layer of the application code.  Null value fetched from database is represented as System.DBNull in .NET.  In order to handle this graciously, a simple check for DBNull should suffice.  The following is an example of a utility function to perform such a check:

public static object DBNullDefault(object objectValue, object defaultValue)
{
   if (!DBNull.Value.Equals(objectValue))
      return objectValue;
   return defaultValue;
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading