You're hitting an overload resolution problem.
new SqlParameter("@DebitNinUnit", 0)
This calls the
SqlParameter(string, SqlDbType)
overload, when you were expecting it to call the
SqlParameter(string, object)
overload.
A literal
0
is implicitly convertible to any
Enum
type, and the compiler decides that this implicit conversion is a better match than boxing the
int
into an
object
.
If you were passing an
int
variable instead, then this would work as expected:
static readonly int Zero = 0;
...
new SqlParameter("@DebitNinUnit", Zero)
However, if you passed a
const
, this would still call the wrong overload.
Demonstration:
Enum Overload Resolution | C# Online Compiler | .NET Fiddle[
^]
As to your second option:
new SqlParameter("@DebitNinUnit", SqlDbType.Int, 0)
In this case, the
0
is the
Size
of the parameter, not its
Value
.
You have three options:
- Pass a variable instead of a literal
0
; - Box the literal zero before calling the constructor;
- Set the
Value
property instead of passing the value to the constructor;
static readonly int Zero = 0;
...
new SqlParameter("@DebitNinUnit", Zero)
new SqlParameter("@DebitNinUnit", (object)0)
new SqlParameter("@DebitNinUnit", SqlDbType.Int) { Value = 0 }