6

I'm using output parameters to return values from a stored procedure.

Declaration in stored procedure is : @GrandTtl DECIMAL(19,3) OUT

The SELECT query is:

SET @GrandTtl = (SELECT TOP 1 Bill_Amount 
                 FROM Tbl_Restaurant_Kitchen_Order_Bill_Details 
                 WHERE Bill_Number = @billno)

For example, the select query returns the value 4087.67 then the output parameter value is returned as 4088 from SQL Server to C#.

Here is the C# code calling the stored procedure:

SqlCommand cmd = new SqlCommand("Sp_RestCC_BillDetails", con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter OutParam26 = cmd.Parameters.Add("@GrandTtl", SqlDbType.Decimal,19);

da = new SqlDataAdapter(cmd);

con.Open();
da.Fill(ds, "dtRestCC_Items");
con.Close();

objRCCBEL.GrandTtlOut = Convert.ToDecimal(cmd.Parameters["@GrandTtl"].Value);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Krishna Thota
  • 6,646
  • 14
  • 54
  • 79

2 Answers2

14

You need to set up the C# parameter as

  1. output -- obviously you've done this
  2. decimal type, with a correct/compatible scale

SqlParameter parm = new SqlParameter("@GrandTtl", SqlDbType.Decimal); 
parm.Precision = 19;
parm.Scale = 3;
parm.Direction = ParameterDirection.Output; 
cmd.Parameters.Add(parm);

If you do not set the scale, the default is 0. Ref: SqlParameter.Scale Property

The number of decimal places to which Value is resolved. The default is 0.

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
  • This works for me thanks. But can we place the `Precision ` and `Scale ` in the source line?? like `SqlDbType.Decimal,19.3);` something like this ??? – Krishna Thota Oct 05 '12 at 05:33
  • You'd need this version of the SqlParameter constructor: http://msdn.microsoft.com/en-us/library/5a10hy4y.aspx Just follow the C# example on the page – RichardTheKiwi Oct 05 '12 at 07:07
0

According to Microsoft decimal(p,s) should work for you. The money and smallmoneyt types are just a subset of decimal with precision of 4 places. So i think your problem comes from the type of the variable that is bound to the OUT parameter in C#.

Desislav Kamenov
  • 1,193
  • 6
  • 13