# Index was outside the bounds of the array in Data Directive Custom Code on UD100

**URL:** <https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160>\
**Category:** Kinetic ERP\
**Tags:** bpm\
**Created:** [March 2, 2023, 10:03pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160 "2023-03-02T22:03:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgross](https://www.epiusers.help/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dgross](https://www.epiusers.help/u/dgross)\
**Post date:** [March 2, 2023, 10:03pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/1 "2023-03-02T22:03:17Z")

</div>

I have a data directive on UD100 to auto increment Key1. The code syntax checks out ok and Key1 is auto incrementing as expected once I manually enter the first records value. However when I run validate I get the error “Index was outside the bounds of the array”. I used code found in an older post that was for a Method Directive but I had issues with the MD so switched to a DD which got me farther - [UD100/UD100A Auto Increment](https://www.epiusers.help/t/ud100-ud100a-auto-increment/44339)

I saw some posts that talked about tuple limit and this is way beyond my technical knowledge. Can anyone help? Below is the full code.

```auto
//— Ice.Tables.UD100

//—

//— Auto Increment Key1 Value for UD100 table

foreach (var ttUD100_Recs in (from ttUD100_Row in ttUD100

where ttUD100_Row.Company == Session.CompanyID

&& string.Equals(ttUD100_Row.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase)

select ttUD100_Row))

{

var ttUD100Row = ttUD100_Recs;

if (ttUD100_Recs != null)

{

Ice.Tables.UD100 UD100;

var UD100_Recs = (from UD100_Row in Db.UD100

where UD100_Row.Company == Session.CompanyID

orderby UD100_Row.Key1 descending

select UD100_Row).FirstOrDefault();

{

var UD100Row = UD100_Recs;

if (UD100_Recs == null)

{

ttUD100Row.Key1 = "100001";

}

else

{

ttUD100Row.Key1 =(System.Convert.ToInt32(UD100_Recs.Key1)+ 1).ToString();

}

}

}

}

```

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [March 2, 2023, 11:32pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/2 "2023-03-02T23:32:10Z")

</div>

Put this in a Pre-Processing on Update or modify it for a data directive as you see fit.

```auto
  int lastRecord_ID = 0;
  Ice.Tables.UD100 lastRecord = null;
  
  try
  {
      lastRecord = (from ud100Rows in Db.UD100
                      orderby ud100Rows.Key1 descending
                      select ud100Rows).First();
  }
  catch {};
  
  if(lastRecord != null) lastRecord_ID = Convert.ToInt32(lastRecord.Key1);
  
  foreach(var record in ds.UD100.Where(rec => rec.RowMod == "A"))
  {
      lastRecord_ID++;
      record.Key1 = lastRecord_ID.ToString();
  }

```

---

<div class="post-metadata">

**Author:** ![dgross](https://www.epiusers.help/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dgross](https://www.epiusers.help/u/dgross)\
**Post date:** [March 6, 2023, 1:47pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/3 "2023-03-06T13:47:54Z")

</div>

Thank you…works great!

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [March 6, 2023, 2:40pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/4 "2023-03-06T14:40:28Z")

</div>

No problem 🙂

If your problem is solved, remember to check the solution checkbox to mark it for others.

---

<div class="post-metadata">

**Author:** ![dgross](https://www.epiusers.help/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dgross](https://www.epiusers.help/u/dgross)\
**Post date:** [April 11, 2023, 9:27pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/5 "2023-04-11T21:27:21Z")

</div>

@klincecum - the custom code you provided worked until you get to 10 and then it keeps assigning the number 10 to each row. I am guessing because the Key1 is a string field and the code is converting the number to a .ToString() but am not sure what to do to solve this. The column in the grid also sorts the 10 wrong since the field is string. Any suggestions?

![image](https://www.epiusers.help/uploads/default/original/3X/d/2/d25c15456a5c5c24c265b8167b91a15546be9b0b.png)

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 11, 2023, 9:29pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/6 "2023-04-11T21:29:41Z")

</div>

It’s been a minute. I probably did something stupid. Let me look at it.

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 11, 2023, 9:30pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/7 "2023-04-11T21:30:26Z")

</div>

Yep, I see it, let me think how to explain/fix it.

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 11, 2023, 9:32pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/8 "2023-04-11T21:32:12Z")

</div>

It’s because they are 1, 2, 3 instead of 01, 02, 03.

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 11, 2023, 9:40pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/9 "2023-04-11T21:40:09Z")

</div>

I guess you’ll have to blow those others out or see if you can modify them.

This should do it I think.

```cs
  int lastRecord_ID = 0;
  Ice.Tables.UD100 lastRecord = null;
  
  try
  {
      lastRecord = (from ud100Rows in Db.UD100
                      orderby ud100Rows.Key1 descending
                      select ud100Rows).First();
  }
  catch {};
  
  if(lastRecord != null) lastRecord_ID = Convert.ToInt32(lastRecord.Key1);
  
  foreach(var record in ds.UD100.Where(rec => rec.RowMod == "A"))
  {
      lastRecord_ID++;
      
      string tempLastRecord = lastRecord_ID.ToString();
      
      if(tempLastRecord.Length == 1) tempLastRecord = "0" + tempLastRecord;
      
      record.Key1 = tempLastRecord;
  }

```

---

<div class="post-metadata">

**Author:** ![dgross](https://www.epiusers.help/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dgross](https://www.epiusers.help/u/dgross)\
**Post date:** [April 12, 2023, 1:48pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/10 "2023-04-12T13:48:29Z")

</div>

@klincecum your latest works until I go over 100 and then I have the same issue.

 ![image](https://www.epiusers.help/uploads/default/original/3X/2/8/28f0911348eedfbd1242d0a1d846983185f15736.png)

I was able to get it to work by using int lastRecord\_ID = 100000;  
Unless you have another suggestion?

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 12, 2023, 3:23pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/11 "2023-04-12T15:23:36Z")

</div>

> [@dgross](#):
>
> your latest works until I go over 100 and then I have the same issue.

Because I didn’t think far enough ahead. I’m dense sometimes.

Give me a minute. We’ll go X amount of digits.

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 12, 2023, 3:39pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/12 "2023-04-12T15:39:52Z")

</div>

```cs
int lastRecord_ID = 0;
Ice.Tables.UD100 lastRecord = null;

try
{
    lastRecord = (from ud100Rows in Db.UD100
                    orderby ud100Rows.Key1 descending
                    select ud100Rows).First();
}
catch {};

if(lastRecord != null) lastRecord_ID = Convert.ToInt32(lastRecord.Key1);

foreach(var record in ds.UD100.Where(rec => rec.RowMod == "A"))
{
    lastRecord_ID++;
    
    int leadingZeros = 10;

    string fmt = new String('0', leadingZeros) + "#";
    
    record.Key1 = lastRecord_ID.ToString(fmt);
}

```

Change `leadingZeros` to your desired value.

---

<div class="post-metadata">

**Author:** ![dgross](https://www.epiusers.help/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dgross](https://www.epiusers.help/u/dgross)\
**Post date:** [April 12, 2023, 5:05pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/13 "2023-04-12T17:05:03Z")

</div>

THANK YOU!!! that works. I left it at 10 leading zeros to be safe. This is basically a sequential Request No that will be printed on a form and the Key1 field is the link from the Kinetic Landing Page to the Detail page. Thank goodness for DMT as I utilized it to Add and Delete records for the UD100 table in bulk to test this.

I apologize for taking so long to find this as I know you answered the initial post a while ago. This is a Project I am working in between dealing with issues (mainly Kinetic). I hadn’t entered records past 10 until yesterday.

 ![image](https://www.epiusers.help/uploads/default/original/3X/c/5/c5b882e1b026eca7070b40bfd60b2cfddccf3e89.png)

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [April 12, 2023, 5:34pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/14 "2023-04-12T17:34:53Z")

</div>

Issue could be orderby key1, which is a string. You need to convert it to an int to get the real number.

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [April 12, 2023, 5:38pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/15 "2023-04-12T17:38:51Z")

</div>

here is a link to the latest way to get next id.

> [@Auto Numbering - Customer ID](https://www.epiusers.help/t/auto-numbering-customer-id/43884/10):
>
> It has been fun seeing the document that i published YEARS ago roll around the internet on how to auto-assign customer/supplier/project ID. I made it to be public for that reason… but it has gone under many iterations. Method 1: I “invented” 25 years ago was to create a phoney customer ID called “Next”… and in the customer record, I bumped the number… but that method is bad because you always had to “hide” this phony customer. Method 2 (I borrowed from someone) was to put a UD Field in the Com…

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 12, 2023, 6:30pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/16 "2023-04-12T18:30:31Z")

</div>

> [@knash](#):
>
> You need to convert it to an int to get the real number.

Tunnel Vision.  
 ![Focus What They Want GIF by Graduation](https://media2.giphy.com/media/6kgEKzleRiVrYJxKeG/giphy.webp?cid=e90edba39t31p3i0480njjc8r8vnswnueefnjikad66vy7fv&rid=giphy.webp&ct=g)

Original code with one minor change:  
`orderby Convert.ToInt32(ud100Rows.Key1) descending`

```cs
int lastRecord_ID = 0;
Ice.Tables.UD100 lastRecord = null;

try
{
    lastRecord = (from ud100Rows in Db.UD100
                    orderby Convert.ToInt32(ud100Rows.Key1) descending
                    select ud100Rows).First();
}
catch {};

if(lastRecord != null) lastRecord_ID = Convert.ToInt32(lastRecord.Key1);

foreach(var record in ds.UD100.Where(rec => rec.RowMod == "A"))
{
    lastRecord_ID++;
    record.Key1 = lastRecord_ID.ToString();
}

```

---

<div class="post-metadata">

**Author:** ![dgross](https://www.epiusers.help/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dgross](https://www.epiusers.help/u/dgross)\
**Post date:** [April 12, 2023, 6:52pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/17 "2023-04-12T18:52:49Z")

</div>

Sorry @klincecum but the latest code is making the Key1 = 1 for all records.  
Original code with one minor change:  
`orderby Convert.ToInt32(ud100Rows.Key1) descending`

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [April 12, 2023, 6:53pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/18 "2023-04-12T18:53:20Z")

</div>

Weird, then use the one that does 🙂

---

<div class="post-metadata">

**Author:** ![knash](https://www.epiusers.help/user_avatar/www.epiusers.help/knash/32/74179_2.png) [@knash](https://www.epiusers.help/u/knash)\
**Post date:** [April 12, 2023, 8:57pm UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/19 "2023-04-12T20:57:46Z")

</div>

Wonder if it this would work?  
lastRecord = (from ud100Rows in Db.UD100.OrderByDescending(i =\> Convert.ToInt32(i.Key1) )  
select ud100Rows).First();

---

<div class="post-metadata">

**Author:** ![dgross](https://www.epiusers.help/letter_avatar_proxy/v4/letter/d/9fc29f/32.png) [@dgross](https://www.epiusers.help/u/dgross)\
**Post date:** [April 13, 2023, 11:23am UTC](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160/20 "2023-04-13T11:23:54Z")

</div>

@knash I tried your latest suggested lastRecord line and it also returns Key1 = 1 for all records.

[Next page](https://www.epiusers.help/t/index-was-outside-the-bounds-of-the-array-in-data-directive-custom-code-on-ud100/100160.md?page=2)
