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?