views:

823

answers:

3

In c#, I use a decimal out DbParameter for a Sql Server stored procedure. I create parameter like that


DbParameter prm = comm.CreateParameter();

prm.ParameterName = "@Price";

prm.DbType = DbType.Decimal;

prm.Direction = ParameterDirection.Output;

comm.Parameters.Add(prm);

//and set the value of comm.Parameters["@Price"] to a variable,

decimal.TryParse(comm.Parameters["@Price"].Value.ToString(), 
  out this.ProdPrice);


But the value of the out parameter is allways rounded.

If i call the same stored procedure from Sql Server Management Studio, i can get this out parameter properly with it's precision

There is not a precision or scale properties on DbParameter. And i have to use DbParameter on System.Data.Common namespace for fetching the data

How can i being able to retieve decimal value with its full precision

Thanks in advance...

+1  A: 

First, you don't need to parse the string to get the Decimal, you should be able to cast directly to it:

this.ProdPrice = (Decimal) comm.Parameters["@Price"].Value;

That being said, what is the precision of the @Price parameter? I believe that SQL Server allows for precision up to 35 places, while the Decimal class only provides precision up to 28 places, so if your parameter has more than 28 places, you won't be able to prevent the rounding.

casperOne
+1  A: 

Have you tried setting Precision and Scale? Note you need to cast first.

Marc Gravell
+1  A: 

Adding to Marc's suggestion, you can change the code to

IDbDataParameter prm = comm.CreateParameter();

Rest of the code should work fine. Precision and Scale properties are "explicit interface implementation" in DbParameter.

shahkalpesh
IDbDataParameter DecimalOut = comm.CreateParameter();DecimalOut.Direction = ParameterDirection.Output;DecimalOut.ParameterName = "@Price";DecimalOut.DbType = DbType.Decimal;DecimalOut.Scale = 4; comm.Parameters.Add(DecimalOut);works properly..Thanks All..
Adam Right