SQL Sends NULL value to a DateTime column

2

I have a form in VB.NET where the user informs the delivery dates and delivery forecast of the order and other information. As he does not yet have, for example, the delivery date because he has not yet been informed by the carrier.

My query takes all information from the form guard in an ArrayList and sends it to the method it sends to the database. I placed a checkbox in the 'Delivery Date' field to send the date if it is checked. But I want to send NULL if it is not checked, but it is giving error, saying that the parameter has no value.

I made these shapes inside the ArrayList before sending them to the method:

Dados.Add("NULL")
Dados.Add("'NULL'")
Dados.Add(Nothing)

'Data' is the name of the ArrayList that the method receives and replaces in the parameters. The part where the value of the delivery date is:

DT_ENTREGA = @DT_ENTREGA

I made a temporary solution until I knew how to do it. I am sending the value 1900-01-01 where it should be NULL, when it finishes executing the query, I send an update to update the value to NULL where that column contains 1900-01-01 . It worked, but I think the procedure is not that.

The method receives an ArrayList with several values and then play in the parameters:

objcmd.Parameters.Add(New SqlParameter("@DT_ENTREGA", Registro(5)))

The update did so:

UPDATE LOGISTICA SET DT_ENTREGA = NULL WHERE DT_ENTREGA = '1900-01-01' AND PEDIDO = @PEDIDO;
    
asked by anonymous 20.06.2016 / 20:59

1 answer

2

In your stored procedure "If you are using a" declare the parameter like this:

@DT_ENTREGA datetime = null 

This way when you pass the parameter Dados.Add("") the table will be null by default.

Another way would be to pass the parameter value to DBNull.Value;

Or SqlDateTime.Null;

I believe they are not the right ways to solve, but it will give you more time to think about how to solve this.

    
20.06.2016 / 22:05