# Custom Code - nested table reads throwing errors on 2026.1

**URL:** <https://www.epiusers.help/t/custom-code-nested-table-reads-throwing-errors-on-2026-1/136124>\
**Category:** Kinetic ERP\
**Tags:** bpm\
**Created:** [July 13, 2026, 7:49pm UTC](https://www.epiusers.help/t/custom-code-nested-table-reads-throwing-errors-on-2026-1/136124 "2026-07-13T19:49:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimsTrak](https://www.epiusers.help/user_avatar/www.epiusers.help/simstrak/32/33313_2.png) [@SimsTrak](https://www.epiusers.help/u/SimsTrak)\
**Post date:** [July 13, 2026, 7:49pm UTC](https://www.epiusers.help/t/custom-code-nested-table-reads-throwing-errors-on-2026-1/136124/1 "2026-07-13T19:49:38Z")

</div>

I’ve got something that is seeming to fail on 2026.1 custom BPM code. I’ve got multiple examples where I’ve got reads of a database table within a foreach loop of other reads, and it’s throwing an error “There is already an open DataReader associated with this Connection which must be closed first.” on the app server log.

This example, I’m reading through PORel records, and in that loop, I’m reading the TranGLC looking for the first GL Account.

```auto
foreach(var ord in ( from rrow in Db.PORel 
            where rrow.Company == wkCompany 
               && rrow.PONum == wkPONum
               && rrow.TranType == "PUR-UKN"
  select new { rrow.PONum, rrow.POLine, rrow.PORelNum } ))
{
    xPONum = ord.PONum.ToString();
    xPOLine = ord.POLine.ToString();
    xPORelNum = ord.PORelNum.ToString();
    var gl = ( from grow in Db.TranGLC 
                where grow.Company == wkCompany 
                   && grow.RelatedToFile == "PORel"
                   && grow.Key1 == xPONum 
                   && grow.Key2 == xPOLine 
                   && grow.Key3 == xPORelNum
      select new { grow.GLAccount } ).FirstOrDefault();
    if (gl == null ) HANDLE NO ACCOUNT HERE 
    else PROCESS ACCOUNT HERE
}

```

When I execute this code, I get the “open datareader” error. However, if I move the read of TranGLC outside the foreach read of PORel - it executes cleanly.

`foreach(var ord in ( from rrow in Db.PORel `  
` where rrow.Company == wkCompany`  
` && rrow.PONum == wkPONum`  
` && rrow.TranType == "PUR-UKN"`  
` select new { rrow.PONum, rrow.POLine, rrow.PORelNum } ))`  
`{`  
` xPONum = ord.PONum.ToString();`  
` xPOLine = ord.POLine.ToString();`  
` xPORelNum = ord.PORelNum.ToString();`  
`}`

`var gl = ( from grow in Db.TranGLC `  
` where grow.Company == wkCompany`  
` && grow.RelatedToFile == "PORel"`  
` && grow.Key1 == xPONum`  
` && grow.Key2 == xPOLine`  
` && grow.Key3 == xPORelNum`  
` select new { grow.GLAccount } ).FirstOrDefault();`  
`if (gl == null ) HANDLE NO ACCOUNT HERE `  
`else PROCESS ACCOUNT HERE`

Obviously, when it’s outside the loop, it’s only checking the last PORel record instead of processing each one.

This isn’t the only place I’m getting this error. I’m getting it on other BPM custom code, haven’t fully tested that but in that BPM also, I’m reading through one table doing processing, and eventually trying to read additional tables.

---

<div class="post-metadata">

**Author:** ![jbooker](https://www.epiusers.help/user_avatar/www.epiusers.help/jbooker/32/83580_2.png) [@jbooker](https://www.epiusers.help/u/jbooker)\
**Post date:** [July 13, 2026, 7:56pm UTC](https://www.epiusers.help/t/custom-code-nested-table-reads-throwing-errors-on-2026-1/136124/2 "2026-07-13T19:56:11Z")

</div>

> [@Entity core framework fix at 2026.100?](https://www.epiusers.help/t/entity-core-framework-fix-at-2026-100/135268/49):
>
> No MARS issues come in if your code has nested calls opening DB objects like: foreach(var ttOrderDtl\_Iterator in ds.OrderDtl.Where(x=\> x.Company == Session.CompanyID && (x.RowMod == "A" || x.RowMod == "U"))) { var Part\_Row = Db.Part.With(LockHint.NoLock).Where(x=\> x.Company == Session.CompanyID && x.PartNum == ttOrderDtl\_Iterator.PartNum && x.CheckBox03).FirstOrDefault(); ... } That is an open query in the foreach AND the Parts call. So the Parts call can be changed to: var Part\_Row = …

---

<div class="post-metadata">

**Author:** ![Randy](https://www.epiusers.help/user_avatar/www.epiusers.help/randy/32/51539_2.png) [@Randy](https://www.epiusers.help/u/Randy)\
**Post date:** [July 13, 2026, 7:57pm UTC](https://www.epiusers.help/t/custom-code-nested-table-reads-throwing-errors-on-2026-1/136124/3 "2026-07-13T19:57:45Z")

</div>

Claude or GPT can help clean that up fast. Just paste your code and ask it to review and fix any MARS issues.
