public void SaveCoupon(string couponname)
        {
           
            OddsCheckerCrawler parent = MdiParent as OddsCheckerCrawler;
            try
            {
                    List<Coupon> couponlist = new List<Coupon>();
                    String msg = String.Empty;
                    if (flowLayoutPanel1.Controls.Count > 0)
                    {
                        int count = flowLayoutPanel1.Controls.Count;
                        Task[] tasks = new Task[count];
                        foreach (Control ctrl in flowLayoutPanel1.Controls)
                        {
                             tasks[ctrl.TabIndex] = Task.Factory.StartNew(() =>
                            {
                            if (ctrl.GetType().Equals(typeof(GroupBox)))
                            {
                                GroupBox grpBox = ctrl as GroupBox;

                                foreach (Control ctrl1 in grpBox.Controls)
                                {
                                    if (ctrl1.GetType().Equals(typeof(FlowLayoutPanel)))
                                    {
                                        FlowLayoutPanel panel = ctrl1 as FlowLayoutPanel;
                                        //DataTable dt = coupon.GetMatchesByCouponId(couponid).Tables[0];
                                        //string test = panel.Name;
                                        //DataRow[] rows = dt.Select("(id = " + panel.Name + ")");
                                        //if (ctrl.GetType().Equals(typeof(DataGridView)))
                                        //if (rows.Length <= 0)
                                        //{
                                        foreach (Control ctrl2 in panel.Controls)
                                        {

                                            if (ctrl2.GetType().Equals(typeof(DataGridView)))
                                            {
                                                //string uniqueid = Guid.NewGuid().ToString().Substring(0, 5);
                                                DataGridView grid = (ctrl2 as DataGridView);
                                                long matchid = Convert.ToInt64(grid.Name.Split(',')[1]);
                                                string teamsname = Helper.GetMatchName(matchid);
                                                //string identifier = "MH.GaaFootballAEid" + Guid.NewGuid().ToString().Substring(0, 5) + "." + teamsname;
                                                for (int i = 0; i < grid.Rows.Count - 1; i++)
                                                {
                                                    int x = i + 1;
                                                    string market = Helper.GetMarketName(Convert.ToInt64(grid.Name.Split(',')[0]));
                                                    string identifier = /*"MH.GaaFootballAEid" + uniqueid + "." + teamsname; */  market.Substring(0, 1) + "S" + x + "id";
                                                    //string market = coupon.GetMarketName(Convert.ToInt64(grid.Name.Split(',')[0]));
                                                    //identifier = identifier + market.Substring(0, 1) + "S" + i+1 + "id";
                                                    couponlist.Add(new Coupon() { Bettingmarketid = Convert.ToInt64(grid.Name.Split(',')[0]), Toals = Convert.ToString(grid.Rows[i].Cells[0].Value), Selection = Convert.ToString(grid.Rows[i].Cells[1].Value), Identifier = identifier });
                                                    couponmarkets++;
                                                }
                                            }
                                        }
                                    }
                                  }
                                }
                            }, TaskCreationOptions.LongRunning);
                        }
                        Task.WaitAll(tasks);
                       
                            if (InvokeRequired)
                            {
                                Action a = () =>
                                {
                                    parent.IsProcessRunning = false;
                                    parent.SetProgress(false);
                                    string couponid = Guid.NewGuid().ToString().Substring(0, 5);
                                    GenerateCoupon coupon = new GenerateCoupon();
                                    while (coupon.IsCouponIdExist(couponid))
                                    {
                                        couponid = Guid.NewGuid().ToString().Substring(0, 5);
                                    }
                                    msg = coupon.InsertCoupon(couponlist, couponname,couponid);
                                    MessageBox.Show(msg);
                                }; BeginInvoke(a);
                            }
                    }
                    else
                    {
                        Action c = () =>
                        {
                            parent.IsProcessRunning = false;
                            parent.SetProgress(false);
                        }; BeginInvoke(c);
                        MessageBox.Show("No available markets found!");
                    }
                }
              
            
            
            catch (Exception ex)
            {
                if (InvokeRequired)
                {
                    Action b = () =>
                    {
                        parent.IsProcessRunning = false;
                        parent.SetProgress(false);
                        MessageBox.Show("An error occured while saving this coupon");
                    }; BeginInvoke(b);
                }
            }




        }
 private void btnsave_Click(object sender, EventArgs e)
 {
     if (lblMatchName.Text == "")
     {
         MessageBox.Show("Please select match first.");
     }
     else
     {
         Coupon2 cup = new Coupon2();
         cup.CouponName = txtcouponname.Text;
         string[] match = lblMatchName.Text.ToString().Split(':');
         cup.MatchID = Convert.ToInt32(match[1].ToString());
         string couponid = Guid.NewGuid().ToString().Substring(0, 5);
         cup.CouponID = couponid;
         cup.IsArchived = false;
         clsCpn = new GenerateCoupon();
         clsCpn.InsertCoupon(cup);
         MessageBox.Show("Coupon Saved.");
         RefreshCoupon(Convert.ToInt32(sport));
     }
 }