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;
                }
            }
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,119 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,026 Reputation points
    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,036 Reputation points
    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