taduh
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?
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
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
1. In your sql, change that column as:
SELECT COALESCE(theDecimalColumn,
2. In your code
Dim decAPYRate As Decimal
Try
If sqlDataReader("decColumn")
decAPYRate = 0
Else
decAPYRate = .GetDecimal(sqlDataReader(
End If
Catch ex As Exception
End Try
or you could just modify the sql
select isnull(decColumn,0)
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1. In your sql, change that column as:
SELECT COALESCE(theDecimalColumn,
2. In your code
Dim decAPYRate As Decimal
Try
If sqlDataReader("decColumn")
decAPYRate = 0
Else
decAPYRate = .GetDecimal(sqlDataReader(
End If
Catch ex As Exception
End Try