Pregunta

To clarify: If a node doesn't exist how do I get the null value inserted into the database.

This is one of 4 places each value/name occurs so I will describe the life of one item to be as clear as possible.

First in the LINQ section setting the variable:

    lastDateToOrder = (string) x.Descendants("lastDateToOrder").FirstOrDefault
    //This will either populate a date or null

Second is the SQL statement:

    string qry = @"Insert into TABLE
                   (lastDateToOrder)
                  values(@lastDateToOrder)";

Finally, setting the parameter:

    qryInsert.Parameters
    .Add("@lastDateToOrder", SqlDbType.DateTime).Value = entry.lastDateToOrder;

The finally section is the part causing problems because I am getting an error saying that @lastDateToOrder is not getting defined and I can see this is the case only when the value is null.

I can't figure out what is wrong and why when entry.lastDateToOrder is carrying a null value it doesn't get set as null.

¿Fue útil?

Solución

Found this answer by digging into another question.

Assign null to a SqlParameter

qryInsert.Parameters
.Add("@lastDateToOrder", SqlDbType.DateTime).Value = (object) entry.lastDateToOrder ?? DBNull.Value
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top