0

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.

1
  • Look at debug what you have in "insertNew" Commented Apr 28, 2012 at 13:18

1 Answer 1

2

It's probably a bug in the code fororderLineRep.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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. As you mentioned I tried adding some debug code, and found that I was looking up the wrong product when I checked the value of ProductFormatName. It was a discount ticket, which is counted as a product in orderlines. This "product / discount ticket" has a name in Product.Name but doens't have a ProductFormatName. Well thanks for helping me locating this stupid mistake :)

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.