Link to home
Start Free TrialLog in
Avatar of gr8life
gr8life

asked on

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

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
Avatar of appari
appari
Flag of India image


posted reply in your old question
Avatar of gr8life
gr8life

ASKER

I am trying the changed code now.
Thanks,
Gr8life
Avatar of gr8life

ASKER

Still got the same error message when I ran the changed code against the 1,000 row data set.
Any suggestions?
Thanks,
Gr8life
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
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gr8life

ASKER

The change worked thank you very much,
Gr8life