date error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhoward
    New Member
    • Aug 2007
    • 18

    date error

    I keep getting this error and I can't seem to correct it.

    System.InvalidC astException: Cast from string "" to type 'Date' is not valid.

    Here's the code.

    Sub InsertTempTable s()

    'Sub proc that inserts info into temp tables

    'Production connection
    Dim DBConn As New SqlConnection(A pplication("DBC onn")) 'Open a new connection
    Dim cmdInsertTempTa bles As New SqlCommand("spI nsertTempFWPerm itTables", DBConn)
    Dim sqldatenull As SqlDateTime
    sqldatenull = SqlDateTime.Nul l
    cmdInsertTempTa bles.CommandTyp e = CommandType.Sto redProcedure

    'Stored proc that inserts record into temp tables

    If lstApplicantMI. SelectedItem.Va lue = "Select" Then
    lstApplicantMI. SelectedItem.Va lue = ""
    End If

    Try
    strrdate = txtrdate.Text
    strddate = txtddate.Text
    strdisplaydate = txtdisplaydate. Text
    strlname = txtApplicantLas tName.Text
    strfname = txtApplicantFir stName.Text
    strMI = lstApplicantMI. SelectedItem.Va lue
    strhomeph = txtHomePhone1.V alue + txtHomePhone2.V alue + txtHomePhone3.V alue
    strworkph = txtBusPhone1.Va lue + txtBusPhone2.Va lue + txtBusPhone3.Va lue
    strPCPhone = txtPurchaseCoPh one1.Value + txtPurchaseCoPh one2.Value + txtPurchaseCoPh one3.Value
    dtdob = txtDOB1.Value + "/" + txtDOB2.Value + "/" + txtDOB3.Value
    strReadyDate = txtreadystand.T ext
    strSSN = txtSSN1.Value + txtSSN2.Value + txtSSN3.Value
    strAddr1 = txtMailingAddre ss1.Text
    strAddr2 = txtMailingAddre ss2.Text
    strCity = txtMailingCity. Text
    strState = lstMailingState .SelectedItem.V alue
    strZip = txtMailingZip.T ext
    strConame = txtBusinessName .Text
    strCoAddr1 = txtBusAddress1. Text
    strCoAddr2 = txtBusAddress2. Text
    strCoCity = txtBusCity.Text
    strCostate = lstBusState.Sel ectedItem.Value
    strCoCounty = lstBusCounty.Se lectedItem.Valu e
    strClass = ddlFWClass.Sele ctedItem.Value
    strSalesID = txtSalesTaxNo.T ext
    strPCName = txtPurchaseCoNa me.Text
    strPCAddr1 = txtPurchaseCoAd dress1.Text
    strPCaddr2 = txtPurchaseCoAd dress2.Text
    strPCCity = txtPurchaseCoCi ty.Text
    strPCState = lstPurchaseCoSt ate.SelectedIte m.Value
    strPCZip = txtPurchaseCoZi p.Text
    strrtime = txtrtime.Text
    strdtime = txtdtime.Text
    strdisplaytime = txtdisplaytime. Text
    strsponsor = txtsponsor.Text
    strconductor = txtconductor.Te xt
    strFWUserID = Session("FWUser ID")
    strTransactionN umber = Session("Transa ctionNumber")

    DBConn.Open()

    With cmdInsertTempTa bles.Parameters

    .Add("@vchmiddl eInitial", strMI)
    .Add("@vchrtime ", strrtime)
    .Add("@vchdtime ", strdtime)
    .Add("@vchdispl aytime", strdisplaytime)
    .Add("@vchLastn ame", strlname)
    .Add("@vchfirst name", strfname)
    .Add("@dtdob", dtdob)
    .Add("@vchSSN", strSSN)
    .Add("@vchAppli cantAddress1", strAddr1)
    .Add("@vchAppli cantAddress2", strAddr2)
    .Add("@vchAppli cantCity", strCity)
    .Add("@vchAppli cantState", strState)
    .Add("@vchAppli cantZip", strZip)
    .Add("@vchAppli cantHomePhone", strhomeph)
    .Add("@vchAppli cantWorkPhone", strworkph)
    .Add("@vchcompa nyname", strConame)
    .Add("@vchcompa nyaddress1", strCoAddr1)
    .Add("@vchcompa nyaddress2", strCoAddr2)
    .Add("@vchcompa nycity", strCoCity)
    .Add("@vchcompa nystate", strCostate)
    .Add("@vchcount y", strCoCounty)
    .Add("@vchFWCla ss", strClass)
    .Add("@vchSales ID", strSalesID)
    '.Add("@vchAppl icationreason", strReason)
    .Add("@vchspons or", strsponsor)
    .Add("@vchcondu ctor", strconductor)
    .Add("@vchFWUse rID", strFWUserID)
    .Add("@vchTrans actionNumber", strTransactionN umber)
    .Add("@vchPurCo Name", strPCName)
    .Add("@vchPurco address1", strPCAddr1)
    .Add("@vchPurco address2", strPCaddr2)
    .Add("@vchPurco city", strPCCity)
    .Add("@vchPurco State", strPCState)
    .Add("@vchPurco Zip", strPCZip)
    .Add("@vchPurco phone", strPCPhone)
    '.Add("@dtenter ed", txttodaydate.Te xt)

    If txttodaydate.Te xt = "" Then
    .Add("@dtentere d", sqldatenull)
    Else
    .Add("@dtentere d", txttodaydate.Te xt)
    End If

    If strrdate = "" Then
    .Add("@dtrdate" , sqldatenull)
    Else
    .Add("@dtrdate" , CDate(strrdate) )
    End If

    If strReadyDate = "" Then
    .Add("@dtStandR eadyDate", sqldatenull)
    Else
    .Add("@dtStandR eadyDate", CDate(strReadyD ate))
    End If

    If strdisplaydate = "" Then
    .Add("@dtdispla ydate", sqldatenull)
    Else
    .Add("@dtdispla ydate", CDate(strdispla ydate))
    End If

    If strddate = "" Then
    .Add("@dtddate" , sqldatenull)
    Else
    .Add("@dtddate" , CDate(strddate) )
    End If

    If strReason = "" Then
    .Add("@vchAppli cationReason", "")
    Else
    .Add("@vchAppli cationReason", strReason)
    End If

    If strdisplaytime = "" Then
    .Add("@vchdispl aytime", sqldatenull)
    Else
    .Add("@vchdispl aytime", strdisplaytime)
    End If

    If strdtime = "" Then
    .Add("@vchdtime ", sqldatenull)
    Else
    .Add("@vchdtime ", strdtime)
    End If

    If strrtime = "" Then
    .Add("@vchrtime ", sqldatenull)
    Else
    .Add("@vchrtime ", strrtime)
    End If

    Dim strAmount As String
    Dim date1 As Date
    Dim date4 As Date
    Dim date3 As Date
    Dim strDateDiff, strdatediff2 As String
    date1 = txttodaydate.Te xt
    date4 = txtddate.Text
    date3 = txtdisplaydate. Text
    strDateDiff = (DateDiff("d", date1, date4))
    strdatediff2 = (DateDiff("d", date1, date3))

    If ddlFWClass.Sele ctedItem.Value = "D" Then
    strAmount = 2000
    End If

    If ddlFWClass.Sele ctedItem.Value = "M" Then
    strAmount = 2000
    End If

    If ddlFWClass.Sele ctedItem.Value = "R" Then
    strAmount = 200
    End If

    If ddlFWClass.Sele ctedItem.Value = "W" Then
    strAmount = 700
    End If

    If ddlFWClass.Sele ctedItem.Value = "SR1" Then
    strAmount = 100
    End If

    If ddlFWClass.Sele ctedItem.Value = "SR2" Then
    strAmount = 200
    End If

    .Add("@mAmount" , CDbl(strAmount) )

    If ddlFWClass.Sele ctedItem.Value = "PD" Then
    If strDateDiff < 10 Then
    strAmount = 100
    Else
    strAmount = 50
    End If
    End If

    If ddlFWClass.Sele ctedItem.Value = "CP" Then
    If strdatediff2 < 10 Then
    strAmount = 200
    Else
    strAmount = 100
    End If
    End If

    End With
    cmdInsertTempTa bles.ExecuteNon Query()
    DBConn.Close()




    Catch er As Exception
    LogError(er.ToS tring, "Applicatio n")
    Exit Try
    Finally
    DBConn.Close()
    End Try
    End Sub

    It was working fine until I entered the datediff code highlighted in bold which I really need. Can someone help me get pass that error?
  • fplesco
    New Member
    • Jul 2007
    • 82

    #2
    Originally posted by dhoward
    I keep getting this error and I can't seem to correct it... It was working fine until I entered the datediff code highlighted in bold which I really need. Can someone help me get pass that error?
    Hey -

    Just to be sure that dates are correct and to avoid confusion and error.

    All you have to do is convert all dates from a string or text object.
    For example, the code below

    date1 = txttodaydate.Te xt
    date4 = txtddate.Text
    date3 = txtdisplaydate. Text

    txtdisplaydate. Text is in text / string data type and then, you tried to transfer it into variables in date format (date3).

    To be sure that the dates are correctly converted, you may try to use CDATE or FORMAT functions

    date4 = CDATE(txtddate. Text)
    date3 = Format(txtdispl aydate.Text,"mm/dd/yyyy") ' or yyyy-mm-dd
    Last edited by Killer42; Aug 15 '07, 08:27 AM. Reason: Shortened quote block

    Comment

    • dhoward
      New Member
      • Aug 2007
      • 18

      #3
      I have tried that already, but here's the weird thing. If I comment out the bold part, it works just fine. But when I choose either CP or PD the date textboxes that I don't need throws the error. For instance if I choose CP, txtddate, which will be empty, throws the error. If I choose PD, txtdisplaydate, which is going the be empty, throws the error. But I have the code that says if textbox = "' then enter as null. It seems as if it it still wants me to enter those values and I've tried writing other code but none of them seem to work.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Can someone explain to me why these are strings? DateDiff returns a number, and comparing numbers as strings is risky at any time. They tend to be compared alphabetically, which nets you strange results. For example, while it is true that 100 > 15, it's also true that "15" > "100". (Anyone who doesn't believe me should try it before responding. Just go to the immediate window and enter ? "15" > "100")

        Comment

        • dhoward
          New Member
          • Aug 2007
          • 18

          #5
          Killer42 what would you suggest because I've tried declaring datediff as integers and that still don't work? I've tried everything and I still can't seem to get it to work.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by dhoward
            Killer42 what would you suggest because I've tried declaring datediff as integers and that still don't work? I've tried everything and I still can't seem to get it to work.
            How about this? I use VB6, so my syntax may be off, but hopefully it'll be close enough to show what I mean...
            [CODE=vb]Dim NumDays As Long
            Select Case ddlFWClass.Sele ctedItem.Value
            Case "PD"
            NumDays = DateDiff("d", CDate(txttodayd ate.Text), CDate(txtddate. Text))
            If NumDays < 10 Then
            strAmount = 100
            Else
            strAmount = 50
            End If
            Case "CP"
            NumDays = DateDiff("d", CDate(txttodayd ate.Text), CDate(txtdispla ydate.Text))
            If NumDays < 10 Then
            strAmount = 200
            Else
            strAmount = 100
            End If
            Case Else
            ' Apparently we don't do anything in this situation.
            End Select[/CODE]

            Comment

            • dhoward
              New Member
              • Aug 2007
              • 18

              #7
              thanks! I'll let you know what happens.

              Comment

              • dhoward
                New Member
                • Aug 2007
                • 18

                #8
                it worked. thank you so much.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by dhoward
                  it worked. thank you so much.
                  Great! Glad we could help. :)

                  Comment

                  Working...