DBNull and Convert.IsDBNull() Options

codeling 1298 - 5544
@2015-12-16 18:17:05

The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column.

Do not confuse the notion of null reference in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null reference means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.

The following example calls the Convert.IsDBNull method to determine whether a database field in a contacts database has a valid value.

private string AddFieldValue(string label, DataRow row, string fieldName)
{                               
   if (!Convert.IsDBNull(row[fieldName]))
      return (string) row[fieldName];
   else
      return String.Empty;
}
 

ncdit -166 - 558
@2015-12-16 18:30:54

The DBNull.Value member represents the sole DBNull object. You can determine whether a value retrieved from a database field is a DBNull value by passing the value of that field to the DBNull.Value.Equals method.

private string AddFieldValue(string label, DataRow row, string fieldName)
{                               
   if (! DBNull.Value.Equals(row[fieldName]))
      return (string) row[fieldName] + " ";
   else
      return String.Empty;
}
Users browsing this topic
Guest