I am trying to write DBNull.Value
using Parameters.AddWithVallue
when an optional parameter (a string
) of a C# method is null
.
public static void Abc(string distrito, string municipio = null)
command.Parameters.AddWithValue("@Municipio", municipio ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Municipio", municipio.Length > 0 ? municipio : (object)DBNull.Value);
However, (object)DBNULL
returns two different values in two different working ways. One writes empty/null and other NULL.