DBNull' to type 'Decimal' is not valid.

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
I am attempting to convert a DBNull value to a decimal (0, 0.0, 0.00 etc).

My current code is

VB.NET:
        If row.Item("Name of row") Is DBNull.Value Then
            NameOfRow = Convert.ToDecimal(0.0)
        End If

When i run my app i pass in the row to convert. Every other datatype works/runs as expected except Decimal. It runs with the abbreviated code below:

VB.NET:
For each row..in dataset....rows
Object = row.table("Table").Item("Name of row")
Next

The Object.... line causes an error. The error is DBNull' to type 'Decimal' is not valid.... Ive tried many attempts to overcome this but cant get this to work. Could anyone guide me?

Thanks
 
I had similar issues with a "costing" module I created.

To resolve it I had to create an object, then convert the object.

Not sure if this will work for you but something like:
VB.NET:
Dim o As Object = row.Item("Name of row")
If Convert.IsDBNull(o) Then
   NameOfRow = Convert.ToDecimal(0.0)
 
I tried that but i get "Field 'Value' of type 'DBNull' is 'ReadOnly'"

When trying ro.IsNull("Name of row") Then ro.Item("Name of row").Value = d, the same error persists?
 
I am attempting to convert a DBNull value to a decimal (0, 0.0, 0.00 etc).

My current code is

VB.NET:
        If row.Item("Name of row") Is DBNull.Value Then
            NameOfRow = Convert.ToDecimal(0.0)
        End If
Take a look at your code here, you're only trying to convert it to a decimal if it is db null, I would think you'd want to convert it if it is not db null.
 
Back
Top