Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Convert.IsDBNull Method

Returns an indication whether the specified object is of type DBNull.

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

'Declaration
Public Shared Function IsDBNull ( _
	value As Object _
) As Boolean
'Usage
Dim value As Object 
Dim returnValue As Boolean 

returnValue = Convert.IsDBNull(value)

Parameters

value
Type: System.Object

An object.

Return Value

Type: System.Boolean
true if value is of type DBNull; otherwise, false.

The IsDBNull method tests whether the value parameter is equal to DBNull.Value. It is equivalent to the following code:

Return DBNull.Value.Equals(value)
NoteNote:

DBNull.Value is used to indicate a value that is missing. It is not equivalent to Nothing or to String.Empty.

The following example uses a SqlDataReader object to retrieve survey data from a database. It assigns each row's field values to an array, and then passes each array element to the IsDBNull method. If the method returns true, the example assigns the string "NA" to the array element. The array is then added to the Rows collection of a System.Windows.Forms.DataGridView control.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   ' Define ADO.NET objects. 
   Dim conn As New SqlConnection(connectionString)
   Dim cmd As New SqlCommand
   Dim dr As SqlDataReader

   ' Open connection, and retrieve dataset.
   conn.Open()

   ' Define Command object.
   cmd.CommandText = "Select * From Responses"
   cmd.CommandType = CommandType.Text
   cmd.Connection = conn

   ' Retrieve data reader.
   dr = cmd.ExecuteReader()

   Dim fieldCount As Integer = dr.FieldCount
   Dim fieldValues(fieldCount - 1) As Object
   Dim headers(fieldCount - 1) As String 

   ' Get names of fields. 
   For ctr As Integer = 0 To fieldCount - 1
      headers(ctr) = dr.GetName(ctr)
   Next 

   ' Set up data grid.
   grid.ColumnCount = fieldCount

   With grid.ColumnHeadersDefaultCellStyle
      .BackColor = Color.Navy
      .ForeColor = Color.White
      .Font = New Font(grid.Font, FontStyle.Bold)
   End With 

   With grid
      .AutoSizeRowsMode = _
          DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
      .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
      .CellBorderStyle = DataGridViewCellBorderStyle.Single
      .GridColor = Color.Black
      .RowHeadersVisible = True 

      For columnNumber As Integer = 0 To headers.Length - 1
         .Columns(columnNumber).Name = headers(columnNumber)
      Next 
   End With 

   ' Get data, replace missing values with "NA", and display it.
   Do While dr.Read()
      dr.GetValues(fieldValues)

      For fieldCounter As Integer = 0 To fieldCount - 1
         If Convert.IsDBNull(fieldValues(fieldCounter)) Then
            fieldValues(fieldCounter) = "NA" 
         End If 
      Next
      grid.Rows.Add(fieldValues)
   Loop
   dr.Close()
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft