locked
Decimal Out parameter issue RRS feed

  • Question

  • I have a stored proc that returns a decimal in the form of an out parameter. (See code below). I'm using SQL server 2005.  I read this out paramter in a C# method as a Decimal in C#.  (See code below) I'm using VS 2008.  The problem is that the value being read it is an Int.  Some how my decimal is getting converted to an int.  I've stpeed through the code and varified that the value in the paramter is an int.  is there some reason i should not be able to use and out paramter to get my data?

    Thanks,
    Wiz

    Code snippets
    Stored proc --

    ALTER PROCEDURE [dbo].[Get_Receipts_For_ShipCredit_Sum]

    @Begin_Date datetime,

    @End_Date datetime,

    @Country_Code varchar(5),

    @Part_System varchar(10),

    @Amount decimal(18,6) out

    AS

    BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from

    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    -- Insert statements for procedure here

    SELECT @Amount = SUM(Extended_Cost)

    FROM dbo.MR_Receipts

    WHERE Country_Code = @Country_Code

    AND Receipt_Status <> 0

    AND ITRN_HIS_DATE BETWEEN @Begin_Date AND @End_Date

    AND Part_System = @Part_System

    END



    C# code snippets

    Here's my declared variable

    SqlParameter amount = new SqlParameter("@Amount", SqlDbType.Decimal);


    Here's the method that gets the value.

    private static void GetAmount(string cnnString, SqlCommand command, SqlParameter amount, MrxReportOutObject mrxOutObj)

    {

    using (SqlConnection connection = new SqlConnection(cnnString))

    {

    command.Connection = connection;

    connection.Open();

    using (SqlDataReader reader = command.ExecuteReader())

    {

    if (amount.Value == DBNull.Value)

    {

    mrxOutObj.Amount = 0;

    }

    else

    {

    mrxOutObj.Amount = Convert.ToDecimal(amount.Value);

    }

    reader.Close();

    reader.Dispose();

    }

    connection.Close();

    connection.Dispose();

    }

    }


    Wiz
    Friday, October 31, 2008 4:43 PM

Answers

  • oh yes, I forgot to tell WizyDig, SqlDbType.Money also works in your case
    Arjun Paudel
    • Marked as answer by WizyDig Friday, October 31, 2008 8:04 PM
    Friday, October 31, 2008 7:57 PM

All replies

  • will float be good to you?

    Try this in parameter

    Dim amount As SqlParameter = New SqlParameter("@Amount", SqlDbType.Float)

    Arjun Paudel
    • Proposed as answer by Arjun Paudel Friday, October 31, 2008 7:03 PM
    Friday, October 31, 2008 5:45 PM
  • mrxOutObj.Amount = Convert.ToDecimal(amount.Value);


    Why are you converting if the parameter is already Decimal? You cannot get Decimal out of any type. Do this:

    mrxOutObj.Amount =(Decimal)amount.Value; provided the maxOutObj.Amount is Decimal.

    AlexB
    Friday, October 31, 2008 7:01 PM
  • I guess I did not make myself clear.  When I step into the code with VS 2008 and look at the value of SQL parameter amount.Value.  It is not a decimal it is an int.  It should be a decimal.  I did not cast it incorrectly  the code mrxOutObj.Amount = Convert.ToDecimal(amount.Value); will convert it to a decimal.  but it has been rounded to an Int by ADO.NET somehow.  When I run the Stored Proc in Sql Server Management Studio it returns a decimal.  AS you can see for my code the data type of the parameter is a Decimal.  I wonder if there is some reason you can not return a decimal through an Out parameter.

    Wiz

    Wiz
    Friday, October 31, 2008 7:15 PM
  • I had same kind of error and I fixed using float, though procedure had decimal type, so I purposed that solution


    Its kind of strange but deciaml returns integer, I have not gone that depth for why...since I was able to continue with float

    Arjun Paudel
    Friday, October 31, 2008 7:18 PM
  • since I was able to continue with float

    You can continue with anything you want but float degrades decimal. Float is absolutely inacceptable for monetary calculations. He calculates money. You should remove the mark: proposed answer from your post.


    AlexB
    Friday, October 31, 2008 7:39 PM
  •  When I run the Stored Proc in Sql Server Management Studio it returns a decimal

    Why can you not use a SP to retrieve the value you need if it works for you?

    I am also thinking, perhaps the SUM function messes things up. What is the type for Extended_cost?

    AlexB
    Friday, October 31, 2008 7:49 PM
  • AlexBB, sum does not messes thing here,  if you just declare decimal in stored proc with some value and return the value in out param, you get integer.

    I agree with you about losing some precision while using float, but float as a double was good for my calculation. Thats what I was going to say

    Thanks


    Arjun Paudel
    Friday, October 31, 2008 7:52 PM
  • oh yes, I forgot to tell WizyDig, SqlDbType.Money also works in your case
    Arjun Paudel
    • Marked as answer by WizyDig Friday, October 31, 2008 8:04 PM
    Friday, October 31, 2008 7:57 PM
  • AlexBB, I think I cant remove my  propose answer,

    @WizyDig, since I dont have rights to remove proposed answer, can you please help me? Thanks

    and please try SqlDbType.Money, if that is suitable to you.

    Thanks

    Arjun Paudel
    Friday, October 31, 2008 8:02 PM
  • Money works.  It's not my favorite answer but since there is some sort of Bug in ADO.NET it's a great work around.  Thanks Arjun.  I markerked the Money post as the answer.
    And no sum does not mess up things it mearly sums any type of numeric data.  It uses the underlying percission of the data types.

    Do you guys have the ability to report this decimal problem as a bug? It most certainly is one.  I'm going back an change my code back to use excuteNonQuery on my command object for performance reasons.  I read a post out on the internet somewhere that said it would work that way but did not help.

    Once again Thanks for the help.  I thought I was going crazy.  I've done this before in previous versions of C# and it worked fine.

    Thanks,
    Wiz
    Friday, October 31, 2008 8:13 PM
  • Friday, October 31, 2008 9:01 PM
  • I think I cant remove my  propose answer, 

    Only the author of the post can do it. Moderators can do it as well. The downside of haphazardous marking incorrect posts as answers (in this case the answer was clearly incorrect) is that it may surface during the search because MS weighs such posts as having higher priority during the queries. Some queries come up with thousands of answers and without such regulation people would have difficulty soring them out.

    At the end of the day you provided another correct answer anyway.

    AlexB
    Friday, October 31, 2008 9:06 PM
  • Hi WizyDig!

    Hope you will like this

    I have found a way to preserve decimal value.....finally

    SqlParameter amount = new SqlParameter("@Amount", SqlDbType.Decimal);
    amount.Scale = 23


    Happy Coding

    Arjun Paudel
    Saturday, November 1, 2008 2:19 AM