This documentation is archived and is not being maintained.

DBNull Class

Represents a nonexistent value. This class cannot be inherited.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class DBNull : ISerializable, 
	IConvertible

The DBNull type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetObjectDataImplements the ISerializable interface and returns the data needed to serialize the DBNull object.
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeCodeGets the TypeCode value for DBNull.
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkToString()Returns an empty string (String.Empty). (Overrides Object.ToString().)
Public methodSupported by the XNA FrameworkToString(IFormatProvider)Returns an empty string using the specified System.IFormatProvider.
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkValueRepresents the sole instance of the DBNull class.
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToBooleanInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToByteInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToCharInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToDateTimeInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToDecimalInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToDoubleInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToInt16Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToInt32Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToInt64Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToSByteInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToSingleInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToTypeInfrastructure. Converts the current DBNull object to the specified type.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToUInt16Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToUInt32Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkIConvertible.ToUInt64Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Top

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. Additionally, COM interop uses the DBNull class to distinguish between a VT_NULL variant, which indicates a nonexistent value, and a VT_EMPTY variant, which indicates an unspecified value.

The DBNull type is a singleton class, which means only one DBNull object exists. The DBNull.Value member represents the sole DBNull object. DBNull.Value can be used to explicitly assign a nonexistent value to a database field, although most ADO.NET data providers automatically assign values of DBNull when a field does not have a valid value. 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. However, some languages and database objects supply methods that make it easier to determine whether the value of a database field is DBNull.Value. These include the Visual Basic IsDBNull function, the Convert.IsDBNull method, the DataTableReader.IsDBNull method, and the IDataRecord.IsDBNull method.

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

The following example calls the DBNull.Value.Equals method to determine whether a database field in a contacts database has a valid value. If it does, the field value is appended to the string output in a label.


private void OutputLabels(DataTable dt)
{
   string label; 

   // Iterate rows of table
   foreach (DataRow row in dt.Rows)
   {
      int labelLen;
      label = String.Empty;
      label += AddFieldValue(label, row, "Title");
      label += AddFieldValue(label, row, "FirstName");
      label += AddFieldValue(label, row, "MiddleInitial");
      label += AddFieldValue(label, row, "LastName");
      label += AddFieldValue(label, row, "Suffix");
      label += "\n";
      label += AddFieldValue(label, row, "Address1");
      label += AddFieldValue(label, row, "AptNo");
      label += "\n";
      labelLen = label.Length;
      label += AddFieldValue(label, row, "Address2");
      if (label.Length != labelLen)
         label += "\n";
      label += AddFieldValue(label, row, "City");
      label += AddFieldValue(label, row, "State");
      label += AddFieldValue(label, row, "Zip");
      Console.WriteLine(label);
      Console.WriteLine();
   }
}

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: