2016-04-11 150 views
1

無法將值NULL插入'描述...'列無法將值NULL插入'描述

任何人都可以幫助我嗎?請高度讚賞.. 這裏是我的小巧代碼插入說明,數量,成本和HSCode,只有說明Colume不工作,說插入價值NULL列'說明。

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(); 
} 
+1

看起來你'Description'列不允許空和你實際提供這樣的價值在incomingShipmentLine的'Description'屬性中。要麼你應該正確填充對象,不要使用非空值,要麼應該將值重寫爲某個空字符串,否則應該將列更改爲接受空值。 –

+0

非常感謝你 - Jakub Szumiato,它的工作...... :) - Namaraj Giri –

回答

1

插入前使用ISNULL屬性。

如果是NULL插入empty值,而不是NULL,因爲你的Description coulmn是NOT NULL

試試這個

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(); 
} 
+0

謝謝@Mark對你的幫助..... :) –

+0

@NamarajGiri快樂編程 –