I am using a returned parameter to display the weight of a bin.
Here is my SQL Server stored procedure;
ALTER PROCEDURE [dbo].[SumScrapBinWeight]
@ScrapBinNumber INT,
@ScrapBinWeightSUM DECIMAL(8,2) OUTPUT
AS
BEGIN
SELECT @ScrapBinWeightSUM = SUM(CalculatedWeightLbs)
FROM ScrapData
WHERE ScrapBinNumber = @ScrapBinNumber
END
This works in SSMS and returns the expected value, when I call this in my C# application I am getting an error
Cast not Valid
on the last line. This has me stumped, if I change Decimal
to Int
, it does not throw the error but it does not display the decimals I want to show.
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MfgDataCollector"].ToString()))
{
DynamicParameters param = new DynamicParameters();
param.Add("@ScrapBinNumber", lbl_TicketNumber.Text);
param.Add("@ScrapBinWeightSUM", dbType: DbType.Int32, direction: ParameterDirection.Output);
conn.Execute("SumScrapBinWeight", param, commandType: CommandType.StoredProcedure);
lbl_BinWeight.Text = param.Get<decimal>("@ScrapBinWeightSUM").ToString();
}
Where am I going wrong here?
The problem is with how you've configured the parameter in your .NET code:
// Note DbType.Int32
param.Add("@ScrapBinWeightSUM", dbType: DbType.Int32, direction: ParameterDirection.Output);
You just need to make that the appropriate data type:
// Note DbType.Decimal
param.Add("@ScrapBinWeightSUM", dbType: DbType.Decimal, direction: ParameterDirection.Output);