选择器中的图像出现数据读取器问题

时间:2018-08-30 14:08:00

标签: c# acumatica

我从Ruslan帖子的下面获取了参考,并实现了预期的功能并且可以按预期工作,但有时会遇到这样的问题,例如“在处理字段ItemRequiresTerms时发生错误:此命令已存在一个与之关联的打开的DataReader,首先关闭。”

How to show images inside selector lookup?

一个库存项目可能有多个图像,我们只需要图标图像。 下面是代码。

public void InventoryItemRowSelecting(PXCache sender, PXRowSelectingEventArgs e)
    {
        var row = e.Row as InventoryItem;
        if (row != null)
        {
            if (!string.IsNullOrEmpty(row.ImageUrl))
            {
                foreach (NoteDoc noteDoc in PXSelectReadonly<NoteDoc, Where<NoteDoc.noteID, Equal<Required<NoteDoc.noteID>>>>.Select(Base, row.NoteID)) // here i got error
                {
                    foreach (UploadFile uploadFile in PXSelectReadonly<UploadFile, Where<UploadFile.fileID, Equal<Required<UploadFile.fileID>>>>.Select(Base, noteDoc.FileID))
                    {
                        if (uploadFile.Name.Contains("icon"))
                        {
                            row.ImageUrl =
                            ControlHelper.GetAttachedFileUrl(null, uploadFile.FileID.ToString());
                            break;
                        }
                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:4)

您需要使用单独的连接范围来在BQL事件处理程序中执行其他RowSelecting语句。

更多详细信息,请参见here

您可以通过使用适当的Join来避免嵌套for循环,

public void InventoryItemRowSelecting(PXCache sender, PXRowSelectingEventArgs e)
{
    var row = e.Row as InventoryItem;
    if (row != null)
    {
        if (!string.IsNullOrEmpty(row.ImageUrl))
        {
            using (new PXConnectionScope())
            {
                UploadFile uploadFile = PXSelectReadonly2<UploadFile, InnerJoin<NoteDoc, On<NoteDoc.fileID, Equal<UploadFile.fileID>>>,
                                                Where<NoteDoc.noteID, Equal<Required<NoteDoc.noteID>>,
                                                And<UploadFile.name, Like<Required<UploadFile.name>>>>>.
                                                Select(Base, row.NoteID, "%icon%");
                row.ImageUrl = (uploadFile != null) ? ControlHelper.GetAttachedFileUrl(null, uploadFile.FileID.ToString()) 
                                                        : null;
            }
        }
    }
}

答案 1 :(得分:-1)

这里已经是一个可以接受的答案,但是我想向您展示示例代码中所使用的深度缩进结构的替代方法。

public void InventoryItemRowSelecting(PXCache sender, PXRowSelectingEventArgs e)
{
    var row = e.Row as InventoryItem;
    if (row == null)
        return;  // Don't proceed when row doesn't exist

    if (string.IsNullOrEmpty(row.ImageUrl))
        return; // Don't proceed when ImageUrl doesn't exist

    foreach (NoteDoc noteDoc in PXSelectReadonly<NoteDoc, Where<NoteDoc.noteID, Equal<Required<NoteDoc.noteID>>>>.Select(Base, row.NoteID)) // here i got error
    {
        foreach (UploadFile uploadFile in PXSelectReadonly<UploadFile, Where<UploadFile.fileID, Equal<Required<UploadFile.fileID>>>>.Select(Base, noteDoc.FileID))
        {
            if (!uploadFile.Name.Contains("icon"))
                continue;  // Skip non-icon files

            row.ImageUrl = ControlHelper.GetAttachedFileUrl(null, uploadFile.FileID.ToString());
        }
    }
}

请注意如何使用更多代码页,而不是将所有内容推到右侧。这是您的原始示例,因此它没有接受的答案中的修复程序,我只想向您展示将来可能要考虑的替代模式。

将其应用于接受的解决方案时,这也是一个巨大的变化:

public void InventoryItemRowSelecting(PXCache sender, PXRowSelectingEventArgs e)
{
    var row = e.Row as InventoryItem;
    if (row == null)
        return;  // Don't proceed when row doesn't exist

    if (string.IsNullOrEmpty(row.ImageUrl))
        return; // Don't proceed when ImageUrl doesn't exist

    using (new PXConnectionScope())
    {
        UploadFile uploadFile = PXSelectReadonly2<UploadFile, InnerJoin<NoteDoc, On<NoteDoc.fileID, Equal<UploadFile.fileID>>>,
                                    Where<NoteDoc.noteID, Equal<Required<NoteDoc.noteID>>,
                                    And<UploadFile.name, Like<Required<UploadFile.name>>>>>.
                                    Select(Base, row.NoteID, "%icon%");
        if (uploadFile == null)
            continue; // Skip non-icon files         

        row.ImageUrl = ControlHelper.GetAttachedFileUrl(null, uploadFile.FileID.ToString()); 
    }
}