private void btnNxtRound_Click(object sender, EventArgs e)
        {
            //NewStartList nl;
            if (cbRound.SelectedIndex < 0)
            {
                MessageBox.Show("����� �� ������");
                return;
            }

            int nextID;
            //bool b = false;
            SqlCommand cmd = new SqlCommand("SELECT l.iid FROM lists l(NOLOCK), lists l1(NOLOCK) WHERE " +
                                            "(l.style=l1.style) AND (l.group_id=l1.group_id) AND (l1.iid=" + listID.ToString() +
                                            ") AND (l.round = '" + cbRound.SelectedItem.ToString() + "')", cn);

            try { nextID = Convert.ToInt32(cmd.ExecuteScalar()); }
            catch { nextID = 0; }
            if (nextID > 0)
            {
                DialogResult dgr = MessageBox.Show("����� �������� ��� ����������.\n�������� ���?",
                                                   "�������� ����������", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dgr == DialogResult.No)
                {
                    return;
                }
                //b = true;
            }
            else
            {
                nextID = (int)StaticClass.GetNextIID("lists", cn, "iid", null);
            }
            DataTable dtTmp = StaticClass.FillResFlash(listID, cn, false, out routeNumber, true, out timeLists, out lists);
            Random    rnd   = new Random();

            cmd.CommandText = "SELECT rankingLead, lateAppl FROM Participants P(NOLOCK) WHERE iid = @iid";
            cmd.Parameters.Add("@iid", SqlDbType.Int);
            List <Starter> data = new List <Starter>();

            foreach (DataRow rdd in dtTmp.Rows)
            {
                if (rdd["��."].ToString() == "Q")
                {
                    Starter st = new Starter();
                    st.iid                  = Convert.ToInt32(rdd["�"]);
                    st.prevPos              = Convert.ToInt32(rdd["pos"]);
                    st.prevStart            = 1;
                    st.random               = rnd.NextDouble();
                    st.ranking              = int.MaxValue;
                    st.lateAppl             = false;
                    cmd.Parameters[0].Value = st.iid;
                    try
                    {
                        SqlDataReader rdss = cmd.ExecuteReader();
                        while (rdss.Read())
                        {
                            try { st.lateAppl = (bool)rdss["lateAppl"]; }
                            catch { }
                            try { st.ranking = (int)rdss["rankingLead"]; }
                            catch { }
                            break;
                        }
                        rdss.Close();
                    }
                    catch { }
                    data.Add(st);
                }
                else
                {
                    break;
                }
            }
            //cmd.CommandText = "SELECT LQ.iid " +
            //                  "  FROM lists LQ(NOLOCK) " +
            //                  "  JOIN lists LF(NOLOCK) ON LQ.style = LF.style " +
            //                  "                       AND LQ.group_id = LF.group_id " +
            //                  "                       AND LQ.round = '������������ 1'" +
            //                  " WHERE LF.iid = " + listID.ToString();

            try
            {
                cmd.CommandText = "SELECT P.iid, P.lateAppl, P.rankingLead " +
                                  "  FROM Participants P(NOLOCK) " +
                                  "  JOIN routeResults R(NOLOCK) ON R.climber_id = P.iid " +
                                  "  JOIN lists        L(NOLOCK) ON L.iid = R.list_id " +
                                  " WHERE L.iid_parent = " + listID.ToString() +
                                  "   AND R.preQf = 1 ";
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Starter s = new Starter();
                    s.iid = Convert.ToInt32(rdr["iid"]);
                    bool exists = false;
                    foreach (Starter st in data)
                    {
                        if (st.iid == s.iid)
                        {
                            exists = true;
                            break;
                        }
                    }
                    if (!exists)
                    {
                        s.lateAppl  = Convert.ToBoolean(rdr["lateAppl"]);
                        s.prevPos   = 0;
                        s.prevStart = 0;
                        s.random    = rnd.NextDouble();
                        try { s.ranking = Convert.ToInt32(rdr["rankingLead"]); }
                        catch { s.ranking = int.MaxValue; }
                        data.Add(s);
                    }
                }
                rdr.Close();
            }
            catch { }


            //foreach(StartListMember stm in stNum)
            //{
            //    StartListMember stmQ;
            //    stmQ.iid = stm.iid;
            //    stmQ.prevPos = stm.prevPos;
            //    cmd.CommandText = "SELECT rankingLead FROM Participants WHERE iid = " + stm.iid.ToString();
            //    try { stmQ.ranking = Convert.ToInt32(cmd.ExecuteScalar()); }
            //    catch { stmQ.ranking = 9999; }
            //    stmQ.rndDouble = stm.rndDouble;
            //    starters.Add(stmQ);
            //}
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@i", SqlDbType.Int);
            cmd.Parameters[0].Value = listID;
            cmd.CommandText         = "SELECT g.name, g.iid FROM lists l INNER JOIN Groups g ON l.group_id=g.iid WHERE l.iid=@i";
            SqlDataReader rd         = cmd.ExecuteReader();
            int           group_id   = 0;
            string        group_name = "";

            while (rd.Read())
            {
                group_id   = Convert.ToInt32(rd[1]);
                group_name = rd[0].ToString();
                break;
            }
            rd.Close();
            //int nextID = (int)StaticMethods.GetNextIID("lists", cn);

            ListTypeEnum newListType = ListTypeEnum.LeadSimple;
            List <int>   childLists  = new List <int>();

            try
            {
                //cmd.Transaction = cn.BeginTransaction();
                cmd.Transaction = cn.BeginTransaction();

                cmd.CommandText         = "INSERT INTO lists(iid,group_id,style,round,online) VALUES (@i,@g,@s,@r,@o)";
                cmd.Parameters[0].Value = nextID;

                cmd.Parameters.Add("@g", SqlDbType.Int);
                cmd.Parameters[1].Value = group_id;

                cmd.Parameters.Add("@s", SqlDbType.VarChar);
                cmd.Parameters[2].Value = "���������";

                cmd.Parameters.Add("@r", SqlDbType.VarChar);
                cmd.Parameters[3].Value = cbRound.SelectedItem.ToString();

                cmd.Parameters.Add("@o", SqlDbType.Bit);
                cmd.Parameters[4].Value = StaticClass.IsListOnline(listID, cn, cmd.Transaction);

                cmd.ExecuteNonQuery();
                cmd.CommandText         = "UPDATE lists SET next_round=@i WHERE iid=@g";
                cmd.Parameters[1].Value = listID;
                cmd.ExecuteNonQuery();
                cmd.CommandText = "UPDATE lists SET prev_round=@g WHERE iid=@i";
                cmd.ExecuteNonQuery();

                //nl = new NewStartList(false, group_name, "���������", cbRound.SelectedItem.ToString());
                //nl.ShowDialog();
                Sorting sorting = new Sorting(data, StartListMode.NotFirstRound, true);
                sorting.ShowDialog();
                if (sorting.Cancel)
                //if (nl.type == StartListType.Cancel)
                {
                    //cmd.Transaction.Rollback();

                    try { cmd.Transaction.Rollback(); }
                    catch
                    {
                        cmd.CommandText = "DELETE FROM lists WHERE iid = " + nextID.ToString();
                        try { cmd.ExecuteNonQuery(); }
                        catch { }
                    }
                    return;
                }

                cmd.Parameters.Clear();
                cmd.CommandText = "UPDATE lists SET listType = @ltp WHERE iid = " + nextID.ToString();
                cmd.Parameters.Add("@ltp", SqlDbType.VarChar, 255);
                newListType             = sorting.RoundFlash ? ListTypeEnum.LeadFlash : ListTypeEnum.LeadSimple;
                cmd.Parameters[0].Value = newListType.ToString();
                cmd.ExecuteNonQuery();

                if (newListType == ListTypeEnum.LeadFlash)
                {
                    cmd.CommandText = "UPDATE lists SET routeNumber = " + sorting.SettedRouteNumber.ToString() +
                                      " WHERE iid = " + nextID.ToString();
                    cmd.ExecuteNonQuery();
                    for (int i = 1; i <= sorting.SettedRouteNumber; i++)
                    {
                        cmd.Parameters.Clear();
                        int curId = (int)StaticClass.GetNextIID("lists", cn, "iid", cmd.Transaction);
                        cmd.CommandText = "INSERT INTO lists(iid,group_id,style,round,online,listType,iid_parent) VALUES (@i,@g,@s,@r,@o,@lt,@ip)";
                        cmd.Parameters.Add("@i", SqlDbType.Int);
                        cmd.Parameters[0].Value = curId;
                        childLists.Add(curId);

                        cmd.Parameters.Add("@g", SqlDbType.Int);
                        cmd.Parameters[1].Value = group_id;

                        cmd.Parameters.Add("@s", SqlDbType.VarChar);
                        cmd.Parameters[2].Value = "���������";

                        cmd.Parameters.Add("@r", SqlDbType.VarChar);
                        cmd.Parameters[3].Value = cbRound.SelectedItem.ToString() + " ������ " + i.ToString();

                        cmd.Parameters.Add("@o", SqlDbType.Bit);
                        cmd.Parameters[4].Value = StaticClass.IsListOnline(listID, cn, cmd.Transaction);

                        cmd.Parameters.Add("@lt", SqlDbType.VarChar, 255);
                        cmd.Parameters[5].Value = ListTypeEnum.LeadSimple.ToString();

                        cmd.Parameters.Add("@ip", SqlDbType.Int);
                        cmd.Parameters[6].Value = nextID;

                        cmd.ExecuteNonQuery();
                    }
                }
                //bool b;

                /*
                 * do
                 * {
                 *  switch (nl.type)
                 *  {
                 *      case StartListType.General:
                 *          starters.Sort(new RandomListComparer());
                 *          b = false;
                 *          break;
                 *      case StartListType.Reverse:
                 *          starters.Sort(new NextRoundComparer());
                 *          b = false;
                 *          break;
                 *      case StartListType.Cancel:
                 *          cmd.Transaction.Rollback();
                 *          return;
                 *      default:
                 *          MessageBox.Show("��� ���������� ����������������. �������� ������ ���.");
                 *          nl.ShowDialog();
                 *          b = true;
                 *          break;
                 *  }
                 * } while (b);*/

                //sorting.Starters
                if (newListType == ListTypeEnum.LeadSimple)
                {
                    childLists.Clear();
                    childLists.Add(nextID);
                }
                for (int i = 0; i < sorting.Count && i < childLists.Count; i++)
                {
                    InsertToList(childLists[i], sorting[i], cn, cmd.Transaction);
                }
                cmd.Transaction.Commit();
                MessageBox.Show("�������� ������. iid = " + nextID.ToString());
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
                try { cmd.Transaction.Rollback(); }
                catch { }
                return;
            }
        }