简体   繁体   中英

LINQ C# Null exception

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 causes orderLine.Product.ProductFormatName to be set to null .

Try adding some debug code:

    foreach (var orderLine in orderlineData)
    {
        if (orderLine.Product.ProductFormatName == null) {
            throw new Exception("ProductFormatName == null");
        }

        // ...

Another strange thing is, why aren't it complaining about:

 ItemName = orderLine.Product.Name, 

This coulmn does not allow nulls either.

I can think of two explanations:

  1. orderLine.Product.Name isn't null. The bug mentioned above may affect only ProductFormatName .
  2. orderLine.Product.Name is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM