-1

We're in the midst of migrating an older C# application to the latest version of C# (and .NET framework) and have come across a problem during testing. Nothing earthshattering - just basic data access code.

Specifically, we're creating a SqlParameter called '@BalanceForward'. In this particular case, we're setting the value to zero - although it could obviously be other values. When executing the stored procedure, we're getting back an error that the procedure expected a parameter called '@BalanceForward' but it wasn't specified. Odd.

We ran a SQL trace and saw the following...

exec usp_Transaction_Create @Id='dummy value',@BalanceForward=default

As you can see, the zero dollar amount is being changed behind the scenes to 'default'. It must be a .NET framework change in behavior - looking at the SQL trace from the version still in production, the zero dollar amount is passed as 0.00.

Is there a way to disable this and just have it pass the actual value instead of 'default'? We don't really want to go through hundreds of stored procedures to account for the possibility that this might occur.

argentntx
  • 23
  • 3
  • 2
    Without the code that triggers this error is difficult to say anything. Pretty sure that this basic functionality has not been changed, otherwise.... – Steve Jun 21 '16 at 21:02
  • 1
    How are you setting the value of the parameter? See http://stackoverflow.com/questions/5356862/sqlparameter-with-default-value-set-to-0-doesnt-work-as-expected – Nikki9696 Jun 21 '16 at 21:07

1 Answers1

0

We were using this code...

cmd.Parameters.Add(DataFactory.CreateParameter("@BalanceForward", 0));

public static DbParameter CreateParameter(string name, object value)
{
   DbParameter param = _dataFactory.CreateParameter();
   param.ParameterName = name;
   if (value == null)
      param.Value = DBNull.Value;
   else
      param.Value = value;
   return param;
}

However - we have now stumbled across the AddWithValue option that was added at some point to the SqlParameter object. We had been using the abstract DbCommand, DbConnection, DbParameter, etc. objects and were passing in the provider via an app.config setting.

Switching from the abstract version to the SqlParameter object and using the AddWithValue option has resolved the issue. Now - it's a matter of figuring out how to do it with the DbParameter object, converting all the abstract code to the System.Data.SqlClient version, or modifying all our stored procedures.

argentntx
  • 23
  • 3