Solved

Invalid cast exception when a datetime field has null value

Posted on 2011-02-11
3
599 Views
Last Modified: 2012-05-11
Hello,
In a VB.Net Windows Forms application I get information from a SQL Server database and display it. Some datetime fields are permitted to be null and I've shown how I handle this in my code in the attached. The procedure which displays all the data is inside a try ... catch block with catch ex as exception (not any more specific exception). Sometimes when users browse a record which has a null expiry_date value they get an unhandled exception the text of which is also attached. It happens inconsistently: one user gets the error when he views a particular record in the application, another user views the same record in the same application, same version, and doesn't get the error. Also if I select continue in the unhandled exception dialogue box then try again to view the relevant tab in the form it works fine.

I assume I need to alter the code shown in the first attachment. I'd be grateful for any help.

Thanks,

Louise
If Not mySqlDataReader.Item("expiry_date") Is DBNull.Value Then myTextBox.Text = mySqlDataReader.Item("expiry_date") Else myTextBox.Text = ""

Open in new window

************** Exception Text **************
System.InvalidCastException: Object cannot be cast from DBNull to other types.
   at System.DBNull.System.IConvertible.ToDateTime(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Windows.Forms.Binding.FormatObject(Object value)
   at System.Windows.Forms.Binding.PushData(Boolean force)
   at System.Windows.Forms.Binding.UpdateIsBinding()
   at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
   at System.Windows.Forms.BindingsCollection.Add(Binding binding)
   at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)
   at System.Windows.Forms.Control.UpdateBindings()
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.TabControl.UpdateTabSelection(Boolean updateFocus)
   at System.Windows.Forms.TabControl.OnSelectedIndexChanged(EventArgs e)
   at System.Windows.Forms.TabControl.WmSelChange()
   at System.Windows.Forms.TabControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Open in new window

0
Comment
Question by:louise001
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
Comment Utility
Due to similar problems, I tend to not use the

If Not IsDbNull() ...


Instead, I use

If IsDBNull(...) = False Then

Now technically its same but the behaviour like you are getting is what forced me into using the later syntax.
0
 
LVL 3

Assisted Solution

by:bc10
bc10 earned 250 total points
Comment Utility
I use the isDate() function to check if the date returned is an actual date.
If isDate(mySqlDataReader.Item("expiry_date") ) then
 TextBox.Text=  mySqlDataReader.Item("expiry_date")
else
  Textbox.Text=""
end if
0
 

Author Closing Comment

by:louise001
Comment Utility
Apologies for having neglected this question, I thought I'd responded but obviously not. Both approaches work so thanks to both of you, I've split the points.

Thanks again,

Louise
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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:…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now