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

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

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.

//— 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();

}

}

}

}

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

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

Thank you…works great!

1 Like

No problem :slight_smile:

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

@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

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

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

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

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

This should do it I think.

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

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

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

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

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

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.

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.

1 Like

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

1 Like

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

Tunnel Vision.
Focus What They Want GIF by Graduation

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

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

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

Weird, then use the one that does :slight_smile:

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

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