473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date error

18 New Member
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?
Aug 14 '07 #1
8 1690
fplesco
82 New Member
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
Aug 15 '07 #2
dhoward
18 New Member
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.
Aug 15 '07 #3
Killer42
8,435 Recognized Expert Expert
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")
Aug 16 '07 #4
dhoward
18 New Member
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.
Aug 20 '07 #5
Killer42
8,435 Recognized Expert Expert
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...
Expand|Select|Wrap|Line Numbers
  1. Dim NumDays As Long
  2. Select Case ddlFWClass.SelectedItem.Value
  3.   Case "PD"
  4.     NumDays = DateDiff("d", CDate(txttodaydate.Text), CDate(txtddate.Text))
  5.     If NumDays < 10 Then
  6.       strAmount = 100
  7.     Else
  8.       strAmount = 50
  9.     End If
  10.   Case "CP"
  11.     NumDays = DateDiff("d", CDate(txttodaydate.Text), CDate(txtdisplaydate.Text))
  12.     If NumDays < 10 Then
  13.       strAmount = 200
  14.     Else
  15.       strAmount = 100
  16.     End If
  17.   Case Else
  18.     ' Apparently we don't do anything in this situation.
  19. End Select
Aug 20 '07 #6
dhoward
18 New Member
thanks! I'll let you know what happens.
Aug 21 '07 #7
dhoward
18 New Member
it worked. thank you so much.
Aug 21 '07 #8
Killer42
8,435 Recognized Expert Expert
it worked. thank you so much.
Great! Glad we could help. :)
Aug 21 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5336
by: kauserk | last post by:
Hello: I have an access database that stores employee timings by date. At the same time, I have a form that queries this database. On the form I use a DTpicker to enable a user input the date for which he/she wants data When I execute the query below in my VB code, I get a run time error that says
10
1897
by: Jack | last post by:
Hi, I cannot get the date format correctly in dynamic sql statement, after trying various ways of handling it. I need some help with the date format in the following dynamic sql statement. Any help is appreciated in advance. While running the asp page, I still get an error as Error Type: Microsoft VBScript runtime (0x800A000D) Type mismatch: 'Format' DYNAMIC SQL STATEMENT:
15
18895
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is
32
5023
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is not a function" ---------------------------------------------------- I have this in a .js file and in the head section.
1
3029
by: JonathanParker | last post by:
Another quick one! Trying to search for records by both accounting period and by year in two seperate queries actioned by option buttons. I've sorted the formatting so it's in a UK format but I keep getting the same error '(3122) you tried to execute a query that does not include the specific expression 'tblAccounting.Period=0406 And tblProduction.DepartmentID=1 ?And tblUtility.UtilityID=1' as part of an aggregate function.' I can't...
6
14373
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future, excluding holidays and weekends.' I didn't want to build a table of holidays that would have to be continuously updated, so I searched high and low for a function that would tell me whether a given date was a holiday, to no avail. I did find an...
3
1626
by: bhavu10 | last post by:
hi i have created a form forreport with today, week month & year CMD Button but i think i wrote the code wrong for month and also it shows me only records for this year whewe as i have 2006 records too code is written in Event Procedure of Respective Cmd Button Can there be a problem in TODAY'S Cmd Button code that it shows me only 2007 record PLEASE HELP. Private Sub cmdMonth_Click()
2
2014
by: LadySugar | last post by:
Greetings! I have a database in access that contains information about the date members joined the company. Every year, membership must be renewed for each member. Usually I will send out a standard letter from a report to each member regardless of the date they joined. Members are only supposed to receive letters an exact year after they joined. I am wondering if it is possible for me to do a setting that will let me to create reports...
5
3447
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to call it. It was suggested to be to have it run through an if statement and if the user enters an invalid date it will kick them to an error page. Here is the date validation code. // date validation using SimpleDateFormat // it will take a...
6
1389
by: DavidPr | last post by:
I have a website that allows people to post ads. Ads remain active for 30 days. In their control panel they can manage their ads, i.e., Edit, Delete, Renew, etc. In this case I'm working with the "Renew" feature. I want to show how many days they have left before their ad becomes inactive. When they submit an ad the date is recorded in the database using MySQL's DATE function. (I'm not interested in the "time", just the date). Here's how I...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.