Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQL Server stored procedure numeric parameter in c#

Tags:

c#

sql

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

like image 801
kashif Avatar asked Dec 16 '22 01:12

kashif


1 Answers

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

like image 162
zimdanen Avatar answered Mar 07 '23 16:03

zimdanen