InvalidCastException when you bind DateTimePicker that contains a null value in Visual Basic (313513)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q313513

SYMPTOMS

When you bind a DateTimePicker control to a data source, you may receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Object cannot be cast from DBNull to other types.

CAUSE

This behavior occurs if the field that is bound to the DateTimePicker control contains a null value that is represented by the System.DBNull object. The DateTimePicker control does not support the System.DBNull object.

RESOLUTION

To avoid this behavior, trap the data before it is displayed in the control, and then change the data so that it is valid.

For example, the following code traps the DBNull value and changes the value to a date value of January 1, 1800. This code uses event handlers with the DateTimePicker control to monitor when the value changes. If the value is null, the value is changed to January 1, 1800.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyDataSet As New DataSet()
    Dim MyTable As New Data.DataTable("dsTable")
    Dim MyColumn As New DataColumn("HireDate")

    MyDataSet.Tables.Add (MyTable)

    MyColumn.AllowDBNull = True
    MyColumn.DataType = GetType(System.DateTime)
    MyTable.Columns.Add (MyColumn)
    Dim MyData(0) As Object
    MyTable.Rows.Add (MyData)

    Dim MyBinding As New Binding("Value", MyDataSet.Tables("dsTable"), "HireDate")
    AddHandler MyBinding.Format, AddressOf DTFormatter 
    AddHandler MyBinding.Parse, AddressOf DTParser 

    DateTimePicker1.DataBindings.Add (MyBinding)

End Sub

Private Sub DTFormatter(ByVal sender As Object, ByVal e As ConvertEventArgs)
    Dim b As Binding = CType(sender, Binding)
    If Not e.DesiredType Is GetType(DateTime) Then
        Return
    End If

    If e.value.GetType Is GetType(System.DBNull) Then
        e.value = CType("1/1/1800", System.DateTime)
    End If
End Sub

Private Sub DTParser(ByVal sender As Object, ByVal e As ConvertEventArgs)
    If Not e.DesiredType Is GetType(DateTime) Then
        Return
    End If
    If Not e.value.GetType Is GetType(DateTime) Then
        Return
    End If

    Dim value As String = CType(e.Value, String)

    If value.Equals("1/1/1800") Then
        e.value = System.DBNull.value
    End If
End Sub
				

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. Create a new Windows Application in Microsoft Visual Basic 2005 or in Microsoft Visual Basic .NET.
  3. Drag a Button control and a DateTimePicker control from the toolbox to the form.
  4. Double-click the Button control to open the code window.
  5. Add the following code to the Click event handler of the Button control.
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim MyDataSet As New DataSet()
        Dim MyTable As New Data.DataTable("dsTable")
        Dim MyColumn As New DataColumn("HireDate")
        Dim MyData(0) As Object
    
        MyDataSet.Tables.Add (MyTable)
        MyColumn.AllowDBNull = True
        MyColumn.DataType = GetType(System.DateTime)
        MyTable.Columns.Add (MyColumn)
        MyTable.Rows.Add (MyData)
        Dim MyBinding As New Binding("Value", MyDataSet.Tables("dsTable"), "HireDate")
        DateTimePicker1.DataBindings.Add (MyBinding)
    End Sub
    					
  6. On the Debug menu, click Start to run the program.
  7. Click the button. Notice that you receive the unhandled exception that is mentioned in the "Symptoms" section.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbprb KB313513 kbAudDeveloper