Results 1 to 5 of 5

Thread: Conversion from string "'5/01/2006'" to type 'Date' is not valid.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    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:
    1. MonthStartDate = ("'" & _
    2.         CStr(MonthCalendar1.SelectionStart.Month) & "/01/" & _
    3.         CStr(MonthCalendar1.SelectionStart.Year) & "'")

    I know it is a type conversion error but I am not sure how to fix it.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. Dim MyString As String = "01/01/2006"
    2.         Dim MyDate As Date = Date.Parse(MyString)
    3.         MessageBox.Show(MyDate.ToLongDateString)
    Last edited by gigemboy; Oct 30th, 2006 at 02:31 PM.

  3. #3
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    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:
    1. 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!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Dim selectionStart As Date = Me.MonthCalendar1.SelectionStart
    2.  
    3. MonthStartDate = New Date(selectionStart.Year, selectionStart.Month, 1)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    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
  •  



Click Here to Expand Forum to Full Width