locked
Error Updating row in DataTable RRS feed

  • Question

  • User2037455357 posted

    Hello there

    I am trying to update a row inside a datatable

    with the below code I get

    System.InvalidOperationException: 'Collection was modified; enumeration operation might not execute.'

    DataTable DT_Scoring = new DataTable();
    
    String AnswerDropDown = "NewRowTest";
    String AnswerRiskScore = "40"; 
    
    
    
    DT_Scoring.Columns.Add("Element");
    DT_Scoring.Columns.Add("Score");
    
                DT_Scoring.Rows.Add(new[]
                        {
                            "Other Low Risk",
                            "3"
    
                        });
    
                DT_Scoring.Rows.Add(new[]
                        {
                            "NewRowTest",
                            "20"
    
                        });
    
                DT_Scoring.Rows.Add(new[]
                        {
                            "Other Medium Risk",
                            "30"
    
                        });
    
    
    
     foreach (DataRow dr in DT_Scoring.Rows) // search whole table
                {
                    if (dr["Element"].ToString() == AnswerDropDown)
                    {
                        dr["Score"] = AnswerRiskScore; //change the score in row
                                            
                    }
    
                    else
                    {
                        DT_Scoring.Rows.Add(new[]
                        {
                            AnswerDropDown,
                            AnswerRiskScore
    
                        });
                    }
    
    
                }

    What am I doing wrong

    All I want to do is, if the row exists in the DataTable I want to update it,

    if it doesn't exist, just insert it.

    Regards

    Rob

    Monday, May 20, 2019 7:58 PM

All replies

  • User475983607 posted

    Try this...

                DataTable DT_Scoring = new DataTable();
    
                String AnswerDropDown = "NewRowTest";
                String AnswerRiskScore = "40";
    
                DT_Scoring.Columns.Add("Element");
                DT_Scoring.Columns.Add("Score");
    
                DT_Scoring.Rows.Add(new[]
                {
                    "Other Low Risk",
                    "3"
                });
    
                DT_Scoring.Rows.Add(new[]
                {
                    "NewRowTest",
                    "20"
                });
    
                DT_Scoring.Rows.Add(new[]
                {
                    "Other Medium Risk",
                    "30"
                });
    
                DataRow[] selectEdit = DT_Scoring.Select($"Element = '{AnswerDropDown}'");
    
                for(int i=0;i<selectEdit.Length; i++)
                {
                    selectEdit[i]["Score"] = AnswerRiskScore;
                }
    
                foreach(DataRow dr in DT_Scoring.Rows)
                {
                    Console.WriteLine($"{dr["Element"]} {dr["Score"]}");
                }

    Monday, May 20, 2019 8:27 PM
  • User-893317190 posted

    Hi masterdineen,

    You are trying to add a new row to datatable in a foreach loop, which is not allowed.

     DT_Scoring.Rows.Add(new[]
                        {
                            AnswerDropDown,
                            AnswerRiskScore
    
                        });

    If you must do that , I suggest that you could use for i loop, like

     for (int i = 0; i < DT_Scoring.Rows.Count; i++)
                {
                    DataRow dr = DT_Scoring.Rows[i];
                    if (dr["Element"].ToString() == AnswerDropDown)
                    {
                        dr["Score"] = AnswerRiskScore; //change the score in row
    
                    }
    
                    else
                    {
                        DT_Scoring.Rows.Add(new[]
                        {
                            AnswerDropDown,
                            AnswerRiskScore
    
                        });
                    }
    
                }

    To be clear , I bind the datatable to a gridview.

      GridView1.DataSource = DT_Scoring;
                GridView1.DataBind();

    Below is the result.

    Best regards,

    Ackerly Xu

    Tuesday, May 21, 2019 2:27 AM
  • User2037455357 posted

    THnak you for this,

    very helpful

    Tuesday, May 21, 2019 6:41 AM