web analytics

DBNull and Convert.IsDBNull()

Options

codeling 1599 - 6654
@2021-03-23 19:26:39

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;
}

@2021-03-23 19:58:27

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;
}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com