Solved

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

Posted on 2004-10-01
3
1,024 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

624 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