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

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.

one last shot in the dark before I try on our end. I am not sure why the conversion on the key1 isn’t working. Are there blanks in the key1 field?

lastRecord = (from ud100Rows in Db.UD100.OrderByDescending(i => int.Parse(i.Key1))
				select ud100Rows).First();

@knash sorry but still assigning 1 to every record.

Yeah, I don’t get it either. I’ll probably futz with it again later.

Don’t worry about us Debbie, that one with the leading zeros should be fine.

We’re just a bit odd…