I am writing a program to read through an Access database and use the records selected to create orders in a separate ERP package... I have successfully selected the records for processing from Access into a dataset, but I am having problems due to InvalidCastException errors.

I have two fields in Access (Price and Cost) that are typed as 'Currency'. The purpose of these two fields are to override the values in the ERP package, that is, if they have a value set in Access, use it, if not, use the default value in the ERP package.

So, it is not unusual for there to be no value set for price and cost. Attempts to access this data from my data set in my code generated the cast exception errors mentioned above. I have created the following as a workaround for this issue, but my question is - Is this the best way to accomplish this? Suggestions are appreciated.

#code

Dim wQty, wPrice, wCost As Double

wPrice = 0
If Not (IsDBNull(ds2.Tables("Dtl").Rows(i).Item("Price"))) Then
wPrice = ds2.Tables("Dtl").Rows(i).Item("Price")
End If

wCost = 0
If ds2.Tables("Dtl").Rows(i).Item("Cost") <> vbNull Then
wCost = ds2.Tables("Dtl").Rows(i).Item("Cost")
End If
#code