4

I'm using the following SQL Server stored procedure in one of my C# projects.

create procedure [dbo].[TestRecordSelect]
@OMarks numeric(3,1) output
as 
begin
select @OMarks = isnull(sum(cast(OMarks as numeric(3,1))),0) from Marks 
end

and I'm getting the desired result i.e. 24.9 when executing the procedure in SQL Server. But when I'm using the same procedure in my C# project and calling the @OMarks as follows

cmd.Parameters.Add("@OMarks", SqlDbType.Decimal).Direction = ParameterDirection.Output;
tbomarks.Text = cmd.Parameters["@OMarks"].Value.ToString();  

I'm getting 25 instead of 24.9 that is the rounded up data.

My problem is I don't want to get 25. I want to get 24.9 in tbomarks

2 Answers 2

7
SqlParameter param = new SqlParameter("@OMarks", SqlDbType.Decimal);
param.Direction = ParameterDirection.Output;
param.Precision = 3;
param.Scale = 1;
cmd.Parameters.Add(param);

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.scale.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

thanks zimdanen now I'm getting the desired result 24.9 in otomarks. thanks again
5

In your C#, you haven't specified the Scale (or Precision) of your parameter, which means you get zero decimal places.

Data may be truncated if the Scale property is not explicitly specified and the data on the server does not fit in scale 0 (the default)

Precision and Scale are required for output parameters

1 Comment

I agree with you :) That'll teach me to get dinner straight after posting instead of improving my answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.