Invoke code activity throw exception in second iteration

I’m using invoke code activity inside for each item in datatable.
It throw exception when second time or in second iteration first time it’s generate output.

Can you share the code inside Invoke Code activity? Otherwise it’s difficult to help.

1 Like

@Anil_Potekar

  • Review the code inside your Invoke Code activity. Make sure there are no runtime errors or unhandled exceptions in the code.
  • Check for any variables or values that may not be initialized or may be null during the second iteration
    *Examine the data in your DataTable, especially during the second iteration. Ensure that the data is in the expected format and that all necessary columns exist.

Cheers!!

1 Like

@Anil_Potekar

Please run in debug and open the exception details from locals panel…you will have the exact error

Cheers

1 Like

Please provide inner.exception

1 Like

Please check the attachment

Hi @Anil_Potekar,

I added a try-catch method to identify errors in the output. Please use the code below. I hope it helps you understand the error. If not, please share the specific error message you are encountering

try
{
    // Get the last date of the previous month
    DateTime today = DateTime.Today;
    DateTime lastDateOfPreviousMonth = new DateTime(today.Year, today.Month, 1).AddDays(-1);

    // Add a new column to the data table
    RRS.Columns.Add("DaysToPreviousMonth", typeof(int));

    // Iterate through each row in the data table
    foreach (DataRow row in RRS.Rows)
    {
        // Get the date value from the "Date" column
		DateTime date = DateTime.ParseExact(row["Date"].ToString(), "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
		
		// Calculate the difference in days
		int daysToPreviousMonth = (lastDateOfPreviousMonth - date).Days;
		
		// Add the difference in days to the new column
		row["DaysToPreviousMonth"] = daysToPreviousMonth;
    }
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}
1 Like

Check the date format, if it’s not in the same format for the second row, then this row in code would be causing the exception

DateTime date = DateTime.ParseExact(row["Date"].ToString(), "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

In that case you could use DateTime.TryParseExact instead and add error handling for the case when parsing fails.

You are attempting to add duplicate columns in the same datatable, which is the reason for the error. Please correct the code if you are using something inside a loop.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.