|
-
Oct 30th, 2006, 02:15 PM
#1
Thread Starter
Hyperactive Member
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.
-
Oct 30th, 2006, 02:28 PM
#2
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)
Last edited by gigemboy; Oct 30th, 2006 at 02:31 PM.
-
Oct 30th, 2006, 05:29 PM
#3
Fanatic Member
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))
Warren Ayen
Senior C# Developer
DLS Software Studios ( http://www.dlssoftwarestudios.com/)
I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
Hey! If you like my post, or I solve your issue, please Rate Me!
-
Oct 30th, 2006, 05:51 PM
#4
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)
-
Oct 30th, 2006, 07:13 PM
#5
Thread Starter
Hyperactive Member
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|