Solved

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

Posted on 2004-10-01
3
1,010 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
Comment Utility
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
Comment Utility
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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