Results 1 to 3 of 3

Thread: Erorr With Date Formats

  1. #1

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    Erorr With Date Formats

    I have a problem

    My Sql db is sat on a server which i guess is in U.S.A format cos dates are mm/dd/yyyy however im in england and the application is going to be used in england which is formatted like dd/mm/yyyy so in my code ive got this
    VB Code:
    1. Dim dteStartDate As Date = CDate(Format(Me.StartDate.Value, "MM/dd/yyyy"))
    2.  
    3. Dim Cmd As New SqlCommand("InsCalAppointment", MyConnection)
    4.         With Cmd
    5.             Try
    6.                 .CommandType = CommandType.StoredProcedure
    7. .Parameters.Add("@dteStartDate", DbType.Date).Value = dteStartDate
    8. .ExecuteNonQuery()
    9.             Catch er As Exception
    10.                 MsgBox(er.ToString)
    11.             End Try

    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from string "12/16/2003" to type 'Date' is not valid.
    Can any one help me plleaseeeee
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Me.StartDate.Value

    ^ Is that actually a date variable coming back from that control (ie a date picker control) ? There is no point doing a cdate(format( as it cancels itself out, one formats to a string, the other converts to a date varaible. Try just using:

    Dim dteStartDate As Date = Me.StartDate.Value

    Because you are using parameters, it wont matter what format you send it, as long as its a datetype (date-types dont actually have formats, just a localized return value).

    Heres how I have done mine, and it works fine: (yes its oledb)

    Code:
     Dim NewItem As New OleDb.OleDbParameter("ParamName", OleDb.OleDbType.Date)
                NewItem.Value = ParamObject
                dbCommand.Parameters.Add(NewItem)

  3. #3

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    Sorted its now done thanks
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

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