I am running a stored procedure with a hardcoded value. The command executes successfully without any errors. But data is not updating in the database. If I run that stored procedure with SQL Server, data is updating. What’s my mistake?
C# code
using (SqlTransaction sqlTrans = con.BeginTransaction())
{
using (SqlCommand AdjustTax = new SqlCommand("GP_SOP_AdjustTax", con, sqlTrans))
{
try
{
AdjustTax.CommandType = CommandType.StoredProcedure;
AdjustTax.Parameters.Add("@IN_SOPType", SqlDbType.Int).Value = 3;
AdjustTax.Parameters.Add("@IN_SOPNo", SqlDbType.VarChar).Value = "stdinv2278";
AdjustTax.Parameters.Add("@IN_AdjustAmount", SqlDbType.Int).Value = 0.04;
AdjustTax.Parameters.Add("@O_iError", SqlDbType.Int, 250);
AdjustTax.Parameters["@O_iError"].Direction = ParameterDirection.Output;
if (con == null || con.State == ConnectionState.Closed)
{
con.Open();
}
AdjustTax.ExecuteNonQuery();
int Error = (int)AdjustTax.Parameters["@O_iError"].Value;
if (Error == 0)
{
MessageBox.Show("Tax is Adjusted");
}
if (Error != 0)
{
MessageBox.Show("Error No:" + Error1);
}
sqlTrans.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
sqlTrans.Rollback();
}
finally
{
con.Close();
}
}
}
SQL Server code
DECLARE @return_value int,
@O_iError int
EXEC @return_value = [dbo].[GP_SOP_AdjustTax]
@IN_SOPType = 3,
@IN_SOPNo = 'stdinv2278',
@IN_AdjustAmount = 0.04,
@O_iError = @O_iError OUTPUT
SELECT @O_iError as N'@O_iError'
SELECT 'Return Value' = @return_value
GO
I think the problem is being caused by this line:
Since the type is
SqlDbType.Intand you are passing in 0.04, that value is most likely getting rounded to 0. Without seeing the contents of the actual stored procedure, I can only guess that passing in a value of 0 for that parameter either causes the stored procedure to skip the update, or the calculation that the stored procedure does results in the column being updated to the same value it originally had.I would try changing that line to:
EDIT
Decimalis the proper mapping for theNumericSql Type, as explained here.