We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Problem OccurredCast from type ‘DBNull’ to type ‘String’ is not valid

gr8life
gr8life asked
on
Medium Priority
331 Views
Last Modified: 2010-04-23
I am working on optimizing an application which utilizes an access database. Just before the application finished processing I received and error message, Problem OccurredCast from type ‘DBNull’ to type ‘String’ is not valid.  I’m not sure what is causing this, however I did some research and it appears to be related to null values in the database.  How do fix this problem?
Thank you for taking the time to read this post,
Gr8life
Comment
Watch Question

Commented:

posted reply in your old question

Author

Commented:
I am trying the changed code now.
Thanks,
Gr8life

Author

Commented:
Still got the same error message when I ran the changed code against the 1,000 row data set.
Any suggestions?
Thanks,
Gr8life

Commented:
have you tried to check using IsDBNull(Value) ? If so, then you can prevent any interaction with that value.

Also, does it give you which field the error is happening on? Also, what exactly are you doing with the db, are you pulling the values in and peforming some operation on it - if so as I pointed out above, use the IsDBNull...like so

Let's say you have a datarow dr
Dim dr As DataRow = ds.Tables(0).rows(0)
Dim Field1Value As String = ""

If isdbNull(Dr.Item("Field1")) Then
  Field1Value = ""
Else
  Field1Value = Dr.Item("Field1")
End IF

HTH
~BC
Commented:
Try this


                For Each dr As datarow In dtInput.rows
                    If NOT IsDBNull(dr.Item(3) ) Then
                  dr.Item("Lookup1") = iplookup(dr.Item(3))
                ELSE
                  dr.Item("Lookup1") = ""
                    END IF
                    If NOT IsDBNull(dr.Item(5) ) Then
                  dr.Item("Lookup2") = iplookup(dr.Item(5))
                ELSE
                  dr.Item("Lookup2") = ""
                    END IF
                   
                    sOutLine = ""
                    For iCnt As Integer = 0 To 15
                        If IsDBNull(dr.Item(iCnt) ) Then
                               sOutLine &=  ","
                        else
                               sOutLine &= dr.Item(iCnt) & ","
                        end if
                        If iCnt = 3 Then
                            sOutLine &= dr.Item("Lookup1") & ","
                        else If iCnt = 5 Then
                            sOutLine &= dr.Item("Lookup2") & ","
                        End If
                    Next
                   

                    sout.WriteLine(sOutLine)

                    lineCounter = lineCounter + 1
                    If lineCounter Mod 10000 = 0 Then
                        sout.Flush()
                    End If

                Next

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
The change worked thank you very much,
Gr8life
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.