Solved

Invalid cast exception when a datetime field has null value

Posted on 2011-02-11
3
602 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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