I’m going to make a few assumptions, but I think I know what’s going on. municipio
is probably an empty string. It is not null.
In that case, municipio ?? (object)DBNull.Value
will be an empty string, not null. However, municipio.Length > 0 ? municipio : (object)DBNull.Value
has a value of DBNull.Value
, which will generate a null in SQL Server. In this case, if municipio
is null, then this code will throw. Since you say the code runs, I’m assuming that municipio
is not null.
6
solved DBNull cast to (object) returns different value [closed]