try
                {
                    string reslt = null;

                    string test;

                        foreach (Control c in flowLayoutPanel1.Controls) //here is the minor change
                        {


                            if (c.GetType() == typeof(ComboBox))
                            {

                                reslt = c.Text;
                                ComboBox combo = (ComboBox)flowLayoutPanel1.Controls.Find(c.Name, true).FirstOrDefault();
                                test = combo.SelectedValue.ToString();


                                string insertrec = "insert into testreport(TestID,result,rang,labno,DetailID)Values('" + refid + "','" + reslt + "','0','" + psta + "','" + test + "')";
                                Program.con.Open();
                                MySqlCommand insertcmd = new MySqlCommand(insertrec, Program.con);
                                insertcmd.ExecuteNonQuery();
                                Program.con.Close();

                            }
                            else if (c.GetType() == typeof(TextBox))
                            {
                                TextBox testname = (TextBox)flowLayoutPanel1.Controls.Find(c.AccessibleName, true).FirstOrDefault();
                                reslt = c.Text;
                                string test1 = c.AccessibleName.ToString();
                                string insertrec = "insert into testreport(TestID,result,rang,labno,DetailID)Values('" + refid + "','" + reslt + "','0','" + psta + "','" + test1 + "')";
                                Program.con.Open();
                                MySqlCommand insertcmd = new MySqlCommand(insertrec, Program.con);
                                insertcmd.ExecuteNonQuery();
                                Program.con.Close();


                            }
                            else if (c.GetType() == typeof(RichTextBox))
                            {

                               // RichTextBoxEx.RichTextBoxEx rich = (RichTextBoxEx.RichTextBoxEx)flowLayoutPanel1.Controls.Find(c.Name,true).FirstOrDefault();
                                // Richtextboxeditor rich=(Richtextboxeditor)flowLayoutPanel1.Controls.Find(c.Name,true).FirstOrDefault();
                                RichTextBox rich = (RichTextBox)flowLayoutPanel1.Controls.Find(c.Name, true).FirstOrDefault();
                                string test2 = c.AccessibleName.ToString();
                                reslt = c.Text.Replace(@"\", @"\\");
                                string insertrec = "insert into testreport(TestID,result,rang,labno,DetailID)Values('" + refid + "','" + reslt + "','0','" + psta + "','" + test2 + "')";
                                Program.con.Open();
                                MySqlCommand insertcmd = new MySqlCommand(insertrec, Program.con);
                                insertcmd.ExecuteNonQuery();
                                Program.con.Close();

                            }

                            }
                        MessageBox.Show("TEST INSERTED SUCCESFULLY !");
                        flowLayoutPanel1.Controls.Clear();

                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }




        }

here is the problem that the loop runs after controls are finished and msg displayed many times.please help me

Dani AI

Generated

Short diagnosis: is correct that break will stop the foreach loop, and / rightly point out continue vs break, but those answers only affect the loop’s flow — they won’t stop the whole method from running multiple times. When the same MessageBox appears repeatedly it usually means the entire handler is being invoked repeatedly (common causes: the event is wired more than once, or the handler is attached to an event that fires for every control removal/addition and you're calling a bulk clear).

Quick checks to reproduce the problem: add a simple counter or breakpoint at the top of the handler to see how many times it enters. Inspect the form’s Designer.cs (or the Events tab in Visual Studio) for duplicate += subscriptions. If the code runs in a ControlRemoved/ControlAdded handler, note that calling a bulk clear will fire that handler once per control.

Practical fixes (examples):

  • Re-entrancy guard (simple and reliable)

    private bool _saving;
    if (_saving) return;
    _saving = true;
    try
    {
      // do save work
    }
    finally
    {
      _saving = false;
    }
  • Detach the event that would re-trigger during a bulk UI change, perform the change, then reattach (or use BeginInvoke to defer UI changes until after the current event completes). Always reset guards in a finally block so the UI isn’t permanently disabled.

Performance/safety notes: open the DB connection once, use a parameterized command and a transaction rather than opening/closing per control; this avoids slowness and SQL injection risks. Ensure exceptions are handled and the re-entry flag or event handlers are restored in finally blocks. These steps stop multiple MessageBoxes and make the save operation robust.

Recommended Answers

All 3 Replies

Use the break statement.

for(loop statement)
{
    break;
}
Member Avatar for Member #1104501
foreach (what you want to loop through) {
    //your work
    if (condition is met) {
        break; //breaks out of the loop
    }
}

If you want to simply skip the current iteration and continue with the next iteration in the foreach loop, you can use continue; instead of break;.

http://stackoverflow.com/questions/6414/c-sharp-loop-break-vs-continue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.