?
Solved

Invalid cast exception when a datetime field has null value

Posted on 2011-02-11
3
Medium Priority
?
617 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 1000 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 1000 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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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