Solved

Invalid cast exception when a datetime field has null value

Posted on 2011-02-11
3
608 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
[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 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 34870163
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
ID: 34870222
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
ID: 35155026
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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