I have a scenario in UiPath, wherein I have a column named ‘ID’ in a DT. It has Unique ID’s uptil 745, as a number.
Now, I want to add more rows in the same DT. When new rows are added it should allott a new ID number , and it should be unique and not match with previous existing ID.
Read Existing Data into dtExisting (use Read Range or populate it in some way)
Initialize highestID as 0
For Each row In dtExisting
If Convert.ToInt32(row(“ID”)) > highestID Then
highestID = Convert.ToInt32(row(“ID”))
Create dtNew (Build Data Table with the same structure as dtExisting)
For Each newDataRow In newRowsToAdd
highestID = highestID + 1
newRow = dtNew.NewRow()
newRow(“ID”) = highestID
’ Set other columns in newRow as needed
dtNew.Rows.Add(newRow)
Read existing data into DataTable
DataTable dt = ReadDataFromSource();
Find the maximum existing ID
int maxExistingID = dt.AsEnumerable().Max(row => Convert.ToInt32(row[“ID”]));
Generate and add new rows with unique IDs
for each newDataRow in NewDataToAdd
{
maxExistingID++; // Increment the maximum ID
DataRow newRow = dt.NewRow();
newRow[“ID”] = maxExistingID;
Set other values for the new row
Add the newRow to the DataTable
dt.Rows.Add(newRow);
}
I am trying to work out the logic. Getting below error, while assigning statement (Assign: YourDataTable.Rows(YourDataTable.Rows.Count - 1)(“UniqueID”) = newRandomNumber)
I have uploaded the FIle and Xaml , can you please have a look, where am I missing here, please?