Wednesday, March 7, 2012

Cannot convert to decimal

I have a money field in SQL that when i try and get the sum of it i cannot convert it to decimal. This was working now its not, and nothing was changed.

Any reason for the error?

DECLARE @.TEST decimal(10,2)
SET @.Test = (SELECT SUM(INV_Net) FROM abc.dbo.iSplit_Details WHERE LoanID='0000010604')
Print @.Test

RETURNS: 160471.24
----------------------------------------------------

Specified cast is not valid.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 3576: // CURRENT TOTAL
Line 3577: cmd.CommandText = "SELECT SUM(INV_Net) FROM abc.dbo.iSplit_Details WHERE LoanID=@.LoanID";
Line 3578: decimal split_currentamt = ((decimal)cmd.ExecuteScalar());

Check if you have NULLs. Its a good idea to set a default of 0 for these type of columns to avoid errors like these.

SELECT SUM(ISNULL(INV_Net,0)) FROM abc.dbo.iSplit_Details WHERE LoanID='0000010604'

|||

that was good information to know, i can use that in a lot of other spots. Thanks

but im still getting the same error.

|||

Was the error at the front end (converting the value to decimal) or at the backend? IF you run the query in query analyzer do you see any errors?

|||

when i run it in query analyzer it works.

|||Oh ok. It wasnt clear if it was a front end issue or a back end one. Can you create a new post again in WebForms section? Sorry for the inconvenience. I dont code in .NET so I may not be able to help you there.|||If you are formatting your currency column in your front end, you may need to strip out the dollar sign($) before you send it back to your database.

No comments:

Post a Comment