Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Invalid cast exception when a datetime field has null value

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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 …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

610 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