Conversion from string "'5/01/2006'" to type 'Date' is not valid.
I am working on converting a VB 2003 to VB 2005 and I have run into this error Conversion from string "'5/01/2006'" to type 'Date' is not valid.
on this peice of code.
VB Code:
MonthStartDate = ("'" & _
CStr(MonthCalendar1.SelectionStart.Month) & "/01/" & _
CStr(MonthCalendar1.SelectionStart.Year) & "'")
I know it is a type conversion error but I am not sure how to fix it.
Re: Conversion from string "'5/01/2006'" to type 'Date' is not valid.
I presume MonthStartDate is a Date object, and you are trying to assign it a String. It doesn't work like that. Look into Date.Parse or Date.TryParse in order to parse a string date into a Date object.
VB Code:
Dim MyString As String = "01/01/2006"
Dim MyDate As Date = Date.Parse(MyString)
MessageBox.Show(MyDate.ToLongDateString)
Re: Conversion from string "'5/01/2006'" to type 'Date' is not valid.
Why are you enclosing it in a '? That's probably why you're having an issue. Try:
VB Code:
MonthStartDate = (CStr(MonthCalendar1.SelectionStart.Month) & "/01/" & CStr(MonthCalendar1.SelectionStart.Year))
Re: Conversion from string "'5/01/2006'" to type 'Date' is not valid.
Why bother turning your numbers into a string and then turning the string into a date? Just turn the numbers straight into a date:
VB Code:
Dim selectionStart As Date = Me.MonthCalendar1.SelectionStart
MonthStartDate = New Date(selectionStart.Year, selectionStart.Month, 1)
Re: Conversion from string "'5/01/2006'" to type 'Date' is not valid.
Some one else wrote the program. I am just doing the converting and in some cases am having a hard time telling what the original author was trying to achieve. Usually VS is able to provide some useful advice in how to fix these problems but in this case it just gives me the error. I tried your suggestion and so far it seems to be doing the trick.
Thanks!