none
Conversion failed when converting datetime from string or out of range vb.net RRS feed

  • Question

  • Mostly got these errors :

    The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.  

    Conversion failed when converting date and/or time from character string. 

    when using 

    strSuccessDate2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ttt")

      strSQL2 = "INSERT INTO tSMSSendLog (fBatchNo, fOrderNo, fRefNo, fDataRefCode, fPriority, fFromID, fMobileNumber," & _
                                            " fMessage, fInputDate, fSuccessDate, fExpireDate, fSendStatus, fSentToMobileNo, fSentFromModemPortNo, " & _
                                            " fResponseText, fDateAdd) VALUES ('" & ds.Tables(0).Rows(0)("fBatchNo") & "', '" & ds.Tables(0).Rows(0)("fOrderNo") & "', '" & ds.Tables(0).Rows(0)("fRefNo") & "', '" & ds.Tables(0).Rows(i)("fDataRefCode").ToString() & "', " & _
                                            ds.Tables(0).Rows(0)("fPriority") & ", '" & ds.Tables(0).Rows(0)("fFromID") & "', '" & ds.Tables(0).Rows(0)("fMobileNumber") & "', '" & ds.Tables(0).Rows(0)("fMessage") & "', '" & _
                                            strInputDate2 & "', '" & strSuccessDate2 & "', '" & ds.Tables(0).Rows(0)("fExpireDate") & "', 'Success', '" & strGetMobileNo & "', 4, '" & RTrim(LTrim(Replace(sResponseText1, "'", ""))) & "', GetDate())"

    where fSuccessDate is datetime datatype,

    not sure it might be the GetDate() which may be the causes,


    • Edited by learner08 Tuesday, May 29, 2012 3:16 AM
    Tuesday, May 29, 2012 3:04 AM

Answers

  • found out that the timedate format in server is actually MM/dd/yyyy

    therefore changed to

     Dim strFailedDate2 As String = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")

    • Marked as answer by learner08 Tuesday, May 29, 2012 7:52 AM
    Tuesday, May 29, 2012 7:52 AM

All replies

  • Hi learner,

    Try to change all date type input one by one with getdate(). In example

    ,'" & input_datetype & "',

    with 

    ,getdate(),
    To know what variable cause an error with presume the getdate() result do not cause an error.

    Agung Gugiaji My application developer journals


    Tuesday, May 29, 2012 7:22 AM
  • found out that the timedate format in server is actually MM/dd/yyyy

    therefore changed to

     Dim strFailedDate2 As String = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")

    • Marked as answer by learner08 Tuesday, May 29, 2012 7:52 AM
    Tuesday, May 29, 2012 7:52 AM