Link to home
Start Free TrialLog in
Avatar of taduh
taduhFlag for United States of America

asked on

What to do when .GetDecimal(#) encounters a null value?

Hi Experts,

I have an SQL field that can contain a decimal value or may contain nulls. Currently, when it contains null and I try to retrieve it, an exception occurs. I would like to be able to handle either situation without an exception occurring if possible. When I encounter a nulll value, I would then like to be able to set the associated textbox on my webpage to blank  - instead of zero.

My code to retrieve the field is attached.

Any suggestions?
Dim decAPYRate As Decimal
                    Try
                        decAPYRate = .GetDecimal(21)
                    Catch ex As Exception
                        decAPYRate = 0
                    End Try

Open in new window

Avatar of nmarun
nmarun
Flag of India image

Two things:

1. In your sql, change that column as:
     SELECT COALESCE(theDecimalColumn, '').... from table1...
2. In your code
     Dim decAPYRate As Decimal
     Try
           If sqlDataReader("decColumn") = string.Empty then
                    decAPYRate = 0
           Else
                    decAPYRate = .GetDecimal(sqlDataReader("decColumn"))
           End If
     Catch ex As Exception
           
      End Try
Slight change

1. In your sql, change that column as:
     SELECT COALESCE(theDecimalColumn, '') as [decColumn] .... from table1...
2. In your code
     Dim decAPYRate As Decimal
     Try
           If sqlDataReader("decColumn") = string.Empty then
                    decAPYRate = 0
           Else
                    decAPYRate = .GetDecimal(sqlDataReader("decColumn"))
           End If
     Catch ex As Exception
           
      End Try
             
 
Avatar of Kyle Abrahams, PMP
or you could just modify the sql

select isnull(decColumn,0)
Yeah I would just use ISNULL(decAPYRate, 0) in the SQL command.  

ISNULL ( check_expression , replacement_value )

Arguments
check_expression
    Is the expression to be checked for NULL. check_expression can be of any type.

replacement_value
    Is the expression to be returned if check_expression is NULL. replacement_value must be of a type that is implicitly convertible to the type of check_expression.
ASKER CERTIFIED SOLUTION
Avatar of taduh
taduh
Flag of United States of America 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