You aren't adding your new SqlParameter
. p
is the result of new SqlParameter("ParentId", SqlDbType.Int).Value = parentId ?? (object) DBNull.Value
. In other words, p
itself is DBNull.Value
.
Split the statement in two, like so:
var p = new SqlParameter("ParentId", SqlDbType.Int);
p.Value = parentId ?? (object) DBNull.Value;
cmd.Parameters.Add(p);
Alternatively,
var p = new SqlParameter("ParentId", SqlDbType.Int) { Value = parentId ?? (object) DBNull.Value };
cmd.Parameters.Add(p);
Either would make sure p
is the parameter, not the parameter's value.