Pregunta

Since it's friday my brain must have malfunctioned.

I'm trying to update a column of the type DateTime with the good ol' OleDb, the update always run (changing other column values on the same row) but for some reason the DateTime column refuses to be set to null.

Ofc the column allows null.

I'm using the OleDbParameter constructor to add a new parameter to my command might it be the parameter that refuses to run if the value is null?

I've tried SqlDateTime.Null, DbNull.Value and null as the parameter but nothing sets the value to null, it leaves it untouched or sets it to our beloved 1900-00-01.

any tips and tricks are appreciated

¿Fue útil?

Solución

you want to use DBNull.Value

just like the prior comments you could do something like DateTime? myDate = null;

Otros consejos

The issue is that unless you take specific action, a DateTime is going to be represented as DateTime.MinValue. So if you want a null in the DB, you will need to test the value and, if MinValue, send DBNull instead.

We actually use a method to get the value as date time or null if date is minvalue:

function object DateToDB(DateTime testDate) 
{
    if (testDate == DateTime.MinValue)
    {
        return DBNull.Value;
    } else 
    {
        return testDate;
    }
}

This method is then used to assign the parameter value.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top