dyarosh
asked on
Invalid Cast Error when inserting data into Database
I am using a TableAdapter to insert data into an MS Access Database and I am getting an Invalid Cast Error when I try and insert a record that has a DBNull value for a date field. The Insert is being populated by the records of another table. I am using the ISDBNull function to check for DBNull and if true, replace the DBNull with 01/01/1900 as the date. So I have two questions:
1. What is wrong with this statement that it doesn't work: (as part of my TableAdapter.Insert call)
IIf(IsDBNull(drRowEmployee s("CheckWr itten")), "#01/01/1900#", drRowEmployees("CheckWritt en")),
IsDBNull(drRowEmployees("C heckWritte n")) = true (verified with MsgBox)
accepts "#01/01/1900#" if I hard-code it
Database field is defined as DateTime and allows Nulls
2. What I really want is if the date field is Null to keep it Null but I can't seem to insert a Null value into the table even though the field is setup to allow null values. Is there a way to do this?
Any help would be greatly appreciated!
1. What is wrong with this statement that it doesn't work: (as part of my TableAdapter.Insert call)
IIf(IsDBNull(drRowEmployee
IsDBNull(drRowEmployees("C
accepts "#01/01/1900#" if I hard-code it
Database field is defined as DateTime and allows Nulls
2. What I really want is if the date field is Null to keep it Null but I can't seem to insert a Null value into the table even though the field is setup to allow null values. Is there a way to do this?
Any help would be greatly appreciated!
Try this ...
IIf(IsDBNull(drRowEmployee s("CheckWr itten")), NULL, drRowEmployees("CheckWritt en"))
ET
IIf(IsDBNull(drRowEmployee
ET
IIf(IsDBNull(drRowEmployee s("CheckWr itten")), "#01/01/1900#", drRowEmployees("CheckWritt en")) <--------------
Since you have the date already hard coded in, could it be that the False parameter is what's throwing the error???
Id so, you will need to enclose the CheckWritten date with ##'s. How do you concatenate with your table adapter??? In Visual Basic you would use the & sign as shown below.
IIf(IsDBNull(drRowEmployee s("CheckWr itten")), "#01/01/1900#", "#" & drRowEmployees("CheckWritt en") & "#")
ET
Since you have the date already hard coded in, could it be that the False parameter is what's throwing the error???
Id so, you will need to enclose the CheckWritten date with ##'s. How do you concatenate with your table adapter??? In Visual Basic you would use the & sign as shown below.
IIf(IsDBNull(drRowEmployee
ET
ASKER
Here is my full Insert. There are other date fields that are not null and they work. I only have the problem when the date field has a null in it:
Me.NewEmployeeEmployerInfo TableAdapt er.Insert( EEID,
drRowEmployees("EmployeeID "),
drRowEmployees("EmployerID "),
IIf(IsDBNull(drRowEmployee s("Departm entNumber" )), "", drRowEmployees("Department Number")),
drRowEmployees("EmployeeTy pe"),
drRowEmployees("EmployeeSt atus"),
IIf(IsDBNull(drRowEmployee s("HireDat e")), "#01/01/1900#", drRowEmployees("HireDate") ),
"#01/01/1900#",
IIf(IsDBNull(drRowEmployee s("Effecti veDateforL ifeInsuran ce")), "#01/01/1900#", drRowEmployees("EffectiveD ateforLife Insurance" )),
drRowEmployees("LifeInsura nceFaceAmo unt"),
IIf(IsDBNull(drRowEmployee s("ActiveD ateInPlan" )), "#01/01/1900#", drRowEmployees("ActiveDate InPlan")),
drRowEmployees("PlanStatus "),
IIf(IsDBNull(drRowEmployee s("PS58")) , 0.0, CDec(drRowEmployees("PS58" ))),
"#01/01/1900#",
"#01/01/1900#",
IIf(IsDBNull(drRowEmployee s("CheckWr itten")), "Null", drRowEmployees("CheckWritt en")),
drRowEmployees("CheckNo"),
0)
Me.NewEmployeeEmployerInfo
drRowEmployees("EmployeeID
drRowEmployees("EmployerID
IIf(IsDBNull(drRowEmployee
drRowEmployees("EmployeeTy
drRowEmployees("EmployeeSt
IIf(IsDBNull(drRowEmployee
"#01/01/1900#",
IIf(IsDBNull(drRowEmployee
drRowEmployees("LifeInsura
IIf(IsDBNull(drRowEmployee
drRowEmployees("PlanStatus
IIf(IsDBNull(drRowEmployee
"#01/01/1900#",
"#01/01/1900#",
IIf(IsDBNull(drRowEmployee
drRowEmployees("CheckNo"),
0)
ASKER
The places where I have #01/01/1900#" are places where the record has a date field that is Null. In trying to figure out where it was failing I had hard coded all of the fields and replaced them one by one. THe CheckWritten field contains a null value. As you can see, I've tried various approaches to get this to work and have not been successful.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did this work
IIf(IsDBNull(drRowEmployee s("CheckWr itten")), NULL, drRowEmployees("CheckWritt en"))
????
ET
IIf(IsDBNull(drRowEmployee
????
ET
ASKER
Don't know why I have to do it this way but it worked! Thank you.
<<<<<Don't know why I have to do it this way but it worked! Thank you.>>>>>
It's probably based on how dates are interpreted or translated in the table adapter going into Access. Storing it to the date variable seems to work.
Just curious ... does this syntax work ... without the ##'s ?
IIf(IsDBNull(drRowEmployee s("CheckWr itten")), "01/01/1900", drRowEmployees("CheckWritt en"))
Thanks for the points.
ET
It's probably based on how dates are interpreted or translated in the table adapter going into Access. Storing it to the date variable seems to work.
Just curious ... does this syntax work ... without the ##'s ?
IIf(IsDBNull(drRowEmployee
Thanks for the points.
ET
Where are you hard coding it in at???
ET