DateDiff error message not understood

Getting an error message when using DateTimePicker in the DateDiff fun

Page 1 of 1

5 Replies - 3636 Views - Last Post: 04 September 2009 - 12:39 PM Rate Topic: -----

#1 wasatchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-September 09

DateDiff error message not understood

Post icon  Posted 03 September 2009 - 06:29 AM

While a fairly simple form it has had its problems. The latest is an error message (see below) that I do not understand and hence cannot correct. I have looked at the other postings, but did not see one that would apply. Any thoughts appreciated. Thanks.


Public Class Form1
	Dim Duration As Decimal

	Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

	End Sub

	Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged

	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalc.Click
		Duration = DateDiff("d", DateTimePicker2, DateTimePicker1)
		Duration = CDec(TxtDuration.Text)

	End Sub

	Private Sub BntClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BntClr.Click
		TxtDuration.Text = ""
	End Sub

End Class



Error message:

System.InvalidCastException was unhandled
Message="Argument 'Date1' cannot be converted to type 'Date'."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.DateAndTime.DateDiff(String Interval, Object Date1, Object Date2, FirstDayOfWeek DayOfWeek, FirstWeekOfYear WeekOfYear)
at DateDifference.Form1.Button1_Click(Object sender, EventArgs e) in D:\My Documents\Visual Studio 2008\Projects\Days\DateDifference\DateDifference\Form1.vb:line 13
at System.Windows.Forms.Control.onclick(EventArgs e)
at System.Windows.Forms.Button.onclick(EventArgs e)
at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at DateDifference.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Again, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: DateDiff error message not understood

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: DateDiff error message not understood

Posted 03 September 2009 - 07:37 AM

DateDiff is expecting a date for the 2nd and 3rd parameter. You need to provide the value from the DateTimePicker, not pass the object itself.

Duration = DateDiff("d", DateTimePicker2.Value, DateTimePicker1.Value)

Was This Post Helpful? 1
  • +
  • -

#3 wasatchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-September 09

Re: DateDiff error message not understood

Posted 03 September 2009 - 08:01 AM

Made the suggested changes - thanks. Now receiving a new error message:

System.InvalidCastException was unhandled
Message="Conversion from string "" to type 'Decimal' is not valid."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDecimal(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDecimal(String Value)
at DateDifference.Form1.Button1_Click(Object sender, EventArgs e) in D:\My Documents\Visual Studio 2008\Projects\Days\DateDifference\DateDifference\Form1.vb:line 14
at System.Windows.Forms.Control.onclick(EventArgs e)
at System.Windows.Forms.Button.onclick(EventArgs e)
at System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at DateDifference.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


Sorry to be a pain. Not sure of the hierarchy of formats. Thought I had solved this earlier. What I am after is an integer (whole days).




View PostJayman, on 3 Sep, 2009 - 06:37 AM, said:

DateDiff is expecting a date for the 2nd and 3rd parameter. You need to provide the value from the DateTimePicker, not pass the object itself.

Duration = DateDiff("d", DateTimePicker2.Value, DateTimePicker1.Value)

Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: DateDiff error message not understood

Posted 03 September 2009 - 08:43 AM

Dumb action... You are overwriting the Duration value in
Duration = CDec(TxtDuration.Text)


where TextDuration.Text contains an empty string that cannot be converted to a Decimal representation. The code you likely wanted is
TxtDuration.Text = Duration.ToString()


Was This Post Helpful? 1
  • +
  • -

#5 wasatchu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-September 09

Re: DateDiff error message not understood

Posted 04 September 2009 - 12:22 PM

That did the trick, thanks. Had to reverse order of DTPs to get positive numbers, otherwise working fine. One can only wonder how difficult will be the next project - adding weeks (delivery) to a date to get another date.

This is a great forum. Wished I had known about it earlier. Would have saved a great deal of frustration.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: DateDiff error message not understood

Posted 04 September 2009 - 12:39 PM

You're over complicating it.
  Dim d1 As Date = New Date(2009, 9, 4)
  Dim ts As TimeSpan = New TimeSpan(10, 0, 0, 0)
  Dim d2 = d1 + ts
  ' d2 is the 14th



Difference between 2 dates
Dim dd As TimeSpan=d2-d1


This post has been edited by AdamSpeight2008: 04 September 2009 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1