How to Run await/async within a For Loop

SupunDev 1 Reputation point
2021-11-18T05:21:39.993+00:00

I'm trying to initiate the following code. There are no errors showing. But these await actions don't work. Can anyone kindly help me to solve this?

private async void TableI_Click(object sender, RoutedEventArgs e)
        {
            MainWindow mainwin = (MainWindow)Window.GetWindow(this);

            List<TblICell> Data = TableIData();
            foreach (TblICell item in Data)
            {
                switch (item.CellID)
                {
                    case 0:
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_SecurityTitle'" + item.RowID + ").value='" + item.CellVal + "'");
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_SecurityTitleFootnote'" + item.RowID + ").value='" + item.FNVal + "'");
                        break;

                    case 1:
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_AmountBeneficiaryOwned'" + item.RowID + ").value='" + item.CellVal + "'");
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_AmountBeneficiaryOwnedFootnote'" + item.RowID + ").value='" + item.FNVal + "'");
                        break;

                    case 2:
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_OwnershipForm'" + item.RowID + ").value='" + item.CellVal + "'");
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_OwnershipFormFootnote'" + item.RowID + ").value='" + item.FNVal + "'");
                        break;

                    case 3:
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_OwnershipNature'" + item.RowID + ").value='" + item.CellVal + "'");
                        await mainwin.MetaWebView.ExecuteScriptAsync($"document.getElementById('Table1_OwnershipNatureFootnote'" + item.RowID + ").value='" + item.FNVal + "'");
                        break;
                }
            }
        }
Developer technologies .NET .NET Runtime
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-11-18T15:43:27.357+00:00

    What does not work mean? It looks like you are trying to execute some JavaScript, but they all have syntax errors. You are appending the rowId after the closing quote in the getElementById

    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-11-18T19:05:42.4+00:00

    Try

    private async Task TableI_Click(object sender, RoutedEventArgs e)

    Never use void with awaiting a task.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.