Solved

Cast from type 'DBNull' to type 'Date' is not valid

Posted on 2004-10-01
3
1,015 Views
Last Modified: 2008-01-09
I am new to .NET and am working on my first application.

Am using a dataset. A date field in the underlying SQL table allows nulls.

I have a web form that uses this field.

When the form encounters a record with a null value in this field I get the following message:

     System.Data.StrongTypingException: Cannot get value because it is DBNull. --->
     System.InvalidCastException: Cast from type 'DBNull' to type 'Date' is not valid. at...

It references the following code:

        Public Property dtDateModified As Date
            Get
                Try
                    Return CType(Me(Me.tableSBS_Property_Group.dtDateModifiedColumn),Date)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableSBS_Property_Group.dtDateModifiedColumn) = value
            End Set
        End Property

I have read several posts here and elsewhere for handling nulls. Unfortunately, I haven't been smart enough to successfully implement any of them.

Your help is appreciated.

John
0
Comment
Question by:JohnDesautels
3 Comments
 
LVL 6

Author Comment

by:JohnDesautels
ID: 12205818
I kept on searching a found the anser at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21123520.html.

This is the code I use in my form now:
(dtDateModified is the field I am checking for nulls)

      If DataSetRow.IsdtDateModifiedNull Then
        lblModifiedData.Text = .sModifiedBy.ToString
      Else
        lblModifiedData.Text = .sModifiedBy.ToString & " - " & .dtDateModified.ToString
      End If

John
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12227435
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now