Problem inserting into Access Database using VB.NET 4.0

Posted on 2012-09-20
Last Modified: 2012-09-21
I have been working on this for days trying to figure out why I am getting this error and can't find it.  I am trying to do an insert into my Access DB using a TableAdapter as shown below:
                                                            IIf(IsDBNull(drRowEmployees("DepartmentNumber")), "", drRowEmployees("DepartmentNumber")),
                                                            IIf(IsDBNull(drRowEmployees("HireDate")), dtDefault, drRowEmployees("HireDate")),
                                                            IIf(IsDBNull(drRowEmployees("TerminationDate")), dtDefault, drRowEmployees("TerminationDate")),
                                                            IIf(IsDBNull(drRowEmployees("EffectiveDateforLifeInsurance")), dtDefault, drRowEmployees("EffectiveDateforLifeInsurance")),
                                                            IIf(IsDBNull(drRowEmployees("LifeInsuranceFaceAmount")), decZero, CDec(drRowEmployees("LifeInsuranceFaceAmount"))),
                                                            IIf(IsDBNull(drRowEmployees("ActiveDateInPlan")), dtDefault, drRowEmployees("ActiveDateInPlan")),
                                                            IIf(IsDBNull(drRowEmployees("BenefitFormsRecevied")), dtDefault, drRowEmployees("BenefitFormsRecevied")),
                                                            IIf(IsDBNull(drRowEmployees("AccountValuation")), dtDefault, drRowEmployees("AccountValuation")),
                                                            IIf(IsDBNull(drRowEmployees("CheckWritten")), dtDefault, drRowEmployees("CheckWritten")),
                                                            IIf(IsDBNull(drRowEmployees("CheckNo")), "", drRowEmployees("CheckNo")),

I am getting the following error and I can't figure out where the error is coming from:
Conversion from type 'DBNull' to type 'Decimal' is not valid.

The only variables that are defined as Decimal are being set to 0.0 using a variable defined as follows:  Dim decZero As Decimal = 0.0

Can someone tell me how to find out which field this is failing on?
Question by:dyarosh
    LVL 33

    Expert Comment

    Have you tried replacing some of the expressions with hard-coded values?

    Obviously that won't solve the problem, but might help find which field the problem is with.
    LVL 84

    Accepted Solution

    You might try String.IsNullOrEmpty instead:

    IIf(String.IsNullOrEmpty(drRowEmployees("DepartmentNumber")), "", drRowEmployees("DepartmentNumber")),

    Or if drRowEmployees is a DataRow, you can try the IsNull method:

    IIF(drRowEmployees.IsNull("DepartmentNumber"), "TrueValue", "FalseValue")

    Author Closing Comment

    I ended up using the String.IsNullOrEmpty and that fixed my problem.  Thanks.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now