decimal DbParameter parameter problem

  • Thread starter Adam The Generic
  • Start date
A

Adam The Generic

Hi All,

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 getting value to a variable
-------------------------------------
decimal.TryParse(comm.Parameters["@Price"].Value.ToString(), out
this.ProdPrice);
-------------------------------------

But the value is allways rounded.

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

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

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

Thanks in advance...
 
H

Hans Kesting

Adam The Generic laid this down on his screen :
Hi All,

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 getting value to a variable
-------------------------------------
decimal.TryParse(comm.Parameters["@Price"].Value.ToString(), out
this.ProdPrice);
-------------------------------------

But the value is allways rounded.

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

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

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

Thanks in advance...

what if you read the value with this code:
decimal ret = (decimal)comm.Parameters["@Price"].Value;

with maybe first a check to see if comm.Parameters["@Price"].Value is
not a DbNull.Value

Hans Kesting
 
N

Norman Yuan

Are you saying the "comm" instance is a DbCommand class? It cannot be,
because DbCommand is an abstract class. I suppose it is an SqlCommand
object, instantiated this way:

DbCommand comm=new SqlCommand(....);

because you cannot do

DbCommand comm=new DbCommand(...); //abstract class cannot be instantiated.

So, comm.CreateParameter() should return an SqlParameter object instance,
which has Precision ans Scale property that you can use here. I do not know
why you HAVE TO refer the parameter as DbParameter, but the parameter
instance itself is an concrete (as opposed to abstract) SqlParameter. Since
you insist to use DbParameter varianble to point to it, your code hides the
Precision/Scale properties.
 
A

Adam The Generic

it was solved by using IDbDataParameter instead of DbParameter .

Thanks..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top