.NET Framework Class Library
DBNull Class

Represents a nonexistent value. This class cannot be inherited.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/934efkk6_en-us_VS_110_2_0_0_0_0%]> _
<[%$TOPIC/934efkk6_en-us_VS_110_2_0_0_0_1%](True)> _
Public NotInheritable Class DBNull _
	Implements [%$TOPIC/934efkk6_en-us_VS_110_2_0_0_0_2%], [%$TOPIC/934efkk6_en-us_VS_110_2_0_0_0_3%]
[[%$TOPIC/934efkk6_en-us_VS_110_2_0_1_0_0%]]
[[%$TOPIC/934efkk6_en-us_VS_110_2_0_1_0_1%](true)]
public sealed class DBNull : [%$TOPIC/934efkk6_en-us_VS_110_2_0_1_0_2%], 
	[%$TOPIC/934efkk6_en-us_VS_110_2_0_1_0_3%]
[[%$TOPIC/934efkk6_en-us_VS_110_2_0_2_0_0%]]
[[%$TOPIC/934efkk6_en-us_VS_110_2_0_2_0_1%](true)]
public ref class DBNull sealed : [%$TOPIC/934efkk6_en-us_VS_110_2_0_2_0_2%], 
	[%$TOPIC/934efkk6_en-us_VS_110_2_0_2_0_3%]
[<[%$TOPIC/934efkk6_en-us_VS_110_2_0_3_0_0%]>]
[<[%$TOPIC/934efkk6_en-us_VS_110_2_0_3_0_1%]>]
[<[%$TOPIC/934efkk6_en-us_VS_110_2_0_3_0_2%](true)>]
type DBNull =  
    class 
        interface [%$TOPIC/934efkk6_en-us_VS_110_2_0_3_0_3%] 
        interface [%$TOPIC/934efkk6_en-us_VS_110_2_0_3_0_4%] 
    end

The DBNull type exposes the following members.

Methods
  NameDescription
Public method Supported by the XNA Framework Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method Supported by the XNA Framework GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetObjectDataImplements the ISerializable interface and returns the data needed to serialize the DBNull object.
Public method Supported by the XNA Framework GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework GetTypeCodeGets the TypeCode value for DBNull.
Public method Supported by the XNA Framework ToStringReturns an empty string ( StringEmpty). (Overrides ObjectToString.)
Public method Supported by the XNA Framework ToString(IFormatProvider)Returns an empty string using the specified SystemIFormatProvider.
Top
Fields
  NameDescription
Public field Static member Supported by the XNA Framework ValueRepresents the sole instance of the DBNull class.
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToBooleanInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToByteInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToCharInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToDateTimeInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToDecimalInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToDoubleInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToInt16Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToInt32Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToInt64Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToSByteInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToSingleInfrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToTypeInfrastructure. Converts the current DBNull object to the specified type.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToUInt16Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToUInt32Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertibleToUInt64Infrastructure. This conversion is not supported. Attempting to make this conversion throws an InvalidCastException.
Top
Remarks

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 DBNullValue member represents the sole DBNull object. DBNullValue 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 DBNullValue. These include the Visual Basic IsDBNull function, the ConvertIsDBNull method, the DataTableReaderIsDBNull method, and the IDataRecordIsDBNull method.

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

Examples

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 Sub OUtputLabels(dt As DataTable)
   Dim label As String  

   ' Iterate rows of table 
   For Each row As DataRow In dt.Rows
      Dim labelLen As Integer
      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 += vbCrLf
      label += AddFieldValue(label, row, "Address1")
      label += AddFieldValue(label, row, "AptNo")
      label += vbCrLf
      labelLen = Len(label)
      label += AddFieldValue(label, row, "Address2")
      If Len(label) <> labelLen Then label += vbCrLf
      label += AddFieldValue(label, row, "City")
      label += AddFieldValue(label, row, "State")
      label += AddFieldValue(label, row, "Zip")
      Console.WriteLine(label)
      Console.WriteLine()
   Next 
End Sub 

Private Function AddFieldValue(label As String, row As DataRow, _
                          fieldName As String) As String 
   If Not DbNull.Value.Equals(row.Item(fieldName)) Then 
      Return CStr(row.Item(fieldName)) & " " 
   Else 
      Return Nothing 
   End If 
End Function
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;
}
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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