Microsoft KB Archive/313513

= InvalidCastException when you bind DateTimePicker that contains a null value in Visual Basic =

Article ID: 313513

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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(&quot;dsTable&quot;) Dim MyColumn As New DataColumn(&quot;HireDate&quot;)

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(&quot;Value&quot;, MyDataSet.Tables(&quot;dsTable&quot;), &quot;HireDate&quot;) 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(&quot;1/1/1800&quot;, 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(&quot;1/1/1800&quot;) Then e.value = System.DBNull.value End If End Sub



STATUS
This behavior is by design.



Steps to reproduce the behavior
 Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. Create a new Windows Application in Microsoft Visual Basic 2005 or in Microsoft Visual Basic .NET. Drag a Button control and a DateTimePicker control from the toolbox to the form. Double-click the Button control to open the code window.  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(&quot;dsTable&quot;) Dim MyColumn As New DataColumn(&quot;HireDate&quot;) 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(&quot;Value&quot;, MyDataSet.Tables(&quot;dsTable&quot;), &quot;HireDate&quot;) DateTimePicker1.DataBindings.Add (MyBinding) End Sub </li> On the Debug menu, click Start to run the program.</li> Click the button. Notice that you receive the unhandled exception that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

Keywords: kbvs2005swept kbvs2005applies kbprb KB313513

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.