Can anyone explain why I’m sometimes gets a NULL exception on this insert method?
As said is only sometimes, which for me is just even more confusing.
The table OrderLine has a referemce to the table Product in the datacontext (.dbml file)
public void insertNewOrder(string accountNumber, int orderId)
{
var order = orderRep.GetOrderById(orderId);
var orderlineData = orderLineRep.GetOrderLines(order.OrderID);
foreach (var orderLine in orderlineData)
{
int currentStatus = dlvRep.getAxOrderStatusNumber(orderLine.ItemNumber, 0);
string levering = "";
string status = dlvRep.getAxOrderStatus(orderLine.ItemNumber, currentStatus, out levering);
WebsiteOrderStatus insertNew = new WebsiteOrderStatus
{
AccountNumber = accountNumber,
OrderID = orderId,
ItemNumber = orderLine.ItemNumber,
ItemName = orderLine.Product.Name,
FormatName = orderLine.Product.ProductFormatName,
Quantity = orderLine.Quantity,
Price = orderLine.Price,
Status = status,
Levering = levering,
LastUpdatedStatus = currentStatus,
CreatedDate = DateTime.Now
};
db.WebsiteOrderStatus.InsertOnSubmit(insertNew);
db.SubmitChanges();
}
}
Exception message:
Cannot insert the value NULL into column 'FormatName', table 'GWportal.dbo.WebsiteOrderStatus'; column does not allow nulls. INSERT fails.
The statement has been terminated.
When I look up the products which this code is having trouble finding the ProductFormatName for. The value of ProductFormatName is not NULL and it’s having the value as I expected ex: “PS3”.
Another strange thing is, why aren’t it complaining about:
ItemName = orderLine.Product.Name,
This coulmn does not allow nulls either.
It’s probably a bug in the code for
orderLineRep.GetOrderLines(order.OrderID)that causesorderLine.Product.ProductFormatNameto be set tonull.Try adding some debug code:
I can think of two explanations:
orderLine.Product.Nameisn’t null. The bug mentioned above may affect onlyProductFormatName.orderLine.Product.Nameis null, but one error is enough to terminate the statement immediately. Only one error will be reported. Other errors that are also present won’t be reported until the first error is fixed.