English 中文(简体)
LINQ C# Null 例外
原标题: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.

该表在数据目录(dbml文档)中载有产品一览表。

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

特殊信息:

Cannot insert the value NULL into column  FormatName , table  GWportal.dbo.WebsiteOrderStatus ; column does not allow nulls. INSERT fails.

声明已终止。

当我看看一下该法典正在发现产品“FormatName”时。 产品产品价值不是NUL,它具有我预期的价值:“PS3”。

另一个奇怪之处是,为什么有人抱怨:

ItemName = orderLine.Product.Name,

这部法律不允许无效。

最佳回答

这可能是<条码>的缩略语:tLineRep.GetOrderLines (order.OrderID)orderLine.Product.ProductFormatName,拟设于null

添加一些条码:

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

        // ...

另一个奇怪之处是,为什么有人抱怨:

ItemName = orderLine.Product.Name,

这部法律不允许无效。

我可以考虑两个解释:

  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.
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签