1

Cannot insert the value NULL into column 'Description...

Can anyone help me ? Please , It will be highly appreciated.. Here is my Dapper Code to insert the Description, Quantity, Cost and HSCode and only the Description Colume is not working saying insert the value NULL into column 'Description.

public void CreateIncomingShipmentLine(IncomingShipmentLine incomingShipmentLine)
{
    DbConnection _Connection = new SqlConnection(Constant.DatabaseConnection);
    _Connection.Open();

    // IncomingShipmentLine

    string myIncomingShipmentLineQuery = "INSERT INTO IncomingShipmentLine( Description, Quantity, Cost, HSCode) VALUES ( @Description, @Quantity, @Cost, @HSCode)";

    _Connection.Execute(myIncomingShipmentLineQuery, new
                                                     {
                                                         Description = incomingShipmentLine.Description,
                                                         Quantity = incomingShipmentLine.Quantity,
                                                         Cost = incomingShipmentLine.Cost,
                                                         HSCode = incomingShipmentLine.HSCode
                                                     });
    _Connection.Close();
}
2
  • 1
    Looks like your Description column doesn't allow nulls and you actually provide such value in the Description property of the incomingShipmentLine. Either you should populate the object properly wth not-null value, or you should override the value to some empty string, or you should change your column to accept nulls. Commented Apr 11, 2016 at 11:06
  • Thank you so much - Jakub Szumiato, Its working ...... :) – Namaraj Giri Commented Apr 11, 2016 at 11:26

1 Answer 1

1

Use ISNULL property before insert.

If it's NULL insert empty value instead of NULL, since your Description coulmn is NOT NULL

Try this

public void CreateIncomingShipmentLine(IncomingShipmentLine incomingShipmentLine)
{
    DbConnection _Connection = new SqlConnection(Constant.DatabaseConnection);
    _Connection.Open();

    // IncomingShipmentLine

    string myIncomingShipmentLineQuery = "INSERT INTO IncomingShipmentLine( Description, Quantity, Cost, HSCode) VALUES ( ISNULL(@Description,''), @Quantity, @Cost, @HSCode)";

    _Connection.Execute(myIncomingShipmentLineQuery, new
                                                     {
                                                         Description = incomingShipmentLine.Description,
                                                         Quantity = incomingShipmentLine.Quantity,
                                                         Cost = incomingShipmentLine.Cost,
                                                         HSCode = incomingShipmentLine.HSCode
                                                     });
    _Connection.Close();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@NamarajGiri happy programming

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.