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

Use the break statement.

for(loop statement)
{
    break;
}
Member Avatar for lithium112
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.