pairCinderella() public method

Used to manually pair a cinderella
public pairCinderella ( int cinderellaID, int fg_id ) : void
cinderellaID int Cinderella's ID#
fg_id int FG's ID#
return void
        private void pairButton_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection personalShopper =  psDGVPairing.SelectedRows;
            DataGridViewSelectedRowCollection cinderella = cinderellaDGVPairing.SelectedRows;
            if ((psDGVPairing.SelectedRows.Count != 1) || (cinderellaDGVPairing.SelectedRows.Count != 1))
            {
                MessageBox.Show("You must select someone.");
                return;
            }
            SQL_Queries dbMagic = new SQL_Queries();
            //bool isPaired = false;
            //string query = dbMagic.getCinderellaStats(Convert.ToInt32(cinderella[0].Cells[0].Value));

            if (dbMagic.CinderellaCurrentStatus(Convert.ToInt32(cinderella[0].Cells[0].Value)) == 3)
            {
                dbMagic.undoFGPairFromCinderellaID(Convert.ToInt32(cinderella[0].Cells[0].Value));
            }
            if (dbMagic.CinderellaCurrentStatus(Convert.ToInt32(cinderella[0].Cells[0].Value)) != 3)
            {
                dbMagic.setCinderellaStatus(cinderella[0].Cells[0].Value.ToString(), 3);
            }
            if (dbMagic.FGCurrentStatus(Convert.ToInt32(personalShopper[0].Cells[0].Value)) == 2)
            {
                dbMagic.undoCinderellaPairFromFGID(Convert.ToInt32(personalShopper[0].Cells[0].Value));
            }
            if (dbMagic.FGCurrentStatus(Convert.ToInt32(personalShopper[0].Cells[0].Value)) != 2)
            {
                dbMagic.setFGStatus(personalShopper[0].Cells[0].Value.ToString(), 2);
            }
            dbMagic.pairCinderella(Convert.ToInt32(cinderella[0].Cells[0].Value),Convert.ToInt32(personalShopper[0].Cells[0].Value));

            /*
            string query = dbMagic.getFgPaired(Convert.ToInt32(Convert.ToInt32(cinderella[0].Cells[0].Value)));
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

            SqlCommand command = new SqlCommand(query, conn);
            conn.Open();
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                if (reader.FieldCount < 1)
                {
                    break;
                }
            dbMagic.setFGStatus(reader.GetInt32(0).ToString(), 4);
            //Thread.Sleep(1000);
            }
            string query2 = dbMagic.getCinderellaPaired(Convert.ToInt32(personalShopper[0].Cells[0].Value));
            SqlCommand command2 = new SqlCommand(query2, conn);
            reader.Close();
            dbMagic.setFGStatus(personalShopper[0].Cells[0].Value.ToString(), 4);
            dbMagic.setCinderellaStatus(Convert.ToInt32(cinderella[0].Cells[0].Value).ToString(), 2);
            dbMagic.clearCinderellaFairyGodmotherID(Convert.ToInt32(cinderella[0].Cells[0].Value));
            //Thread.Sleep(1000);
            SqlDataReader reader2 = command2.ExecuteReader();
            while (reader2.Read())
            {
                if (reader2.FieldCount < 1)
                {
                    break;
                }
                int id = reader2.GetInt32(0);
                dbMagic.setCinderellaStatus(id.ToString(), 2);
                dbMagic.clearCinderellaFairyGodmotherID(id);
              //  Thread.Sleep(1000);
            }
            reader2.Close();
            dbMagic.pairCinderella(Convert.ToInt32(cinderella[0].Cells[0].Value), Convert.ToInt32(personalShopper[0].Cells[0].Value));
            dbMagic.setCinderellaStatus(cinderella[0].Cells[0].Value.ToString(), 3);
            dbMagic.setFGStatus(personalShopper[0].Cells[0].Value.ToString(), 2);
               // Thread.Sleep(1000);
            conn.Close();*/

            MessageBox.Show("Pairing Complete");
            guiupdate();
        }
        //main
        //Purpose: to handle the database interactions and the loops that occur for matchmaking.
        //input: Only the database interactions.
        //Output: Updates are sent back to the database.
        //preconditions: this program is not being made anywhere and the database is functional.
        //postconditions: the database has been left in a consistent state, all matches have been updated to the database, and matchmaking is no longer running.
        static void Main()
        {
            try
            {
                //creates two lists to keep track of cinderellas and godmothers who have been matched.
                List<int> oldCinderellas = new List<int>();
                List<int> oldGodMothers = new List<int>();
                while (true)
                {
                    //creates lists to hold the cinderellas/godmothers pulled down from the database.
                    //theese will be "deleted" at the start of each loop
                    List<CinderellaClass> Cinderella = new List<CinderellaClass>();
                    List<FairyGodmother> GodMother = new List<FairyGodmother>();
                    //holds the unsorted cinderellas
                    List<CinderellaClass> UnsortCinderella = new List<CinderellaClass>();
                    //creates an object to do the matchmaking
                    Program match = new Program();
                    //gets the cinderellas from the db
                    match.GetCinderellas(ref UnsortCinderella, Cinderella.Count, ref oldCinderellas);
                    //gets the godmothers from the db
                    match.GetGodMothers(ref GodMother, GodMother.Count, ref oldGodMothers);

                    //list to hold the matchedcinderellas from the DB
                    List<CinderellaClass> MatchedCinderella = new List<CinderellaClass>();
                    match.GetPairedCinderellas(ref MatchedCinderella); //get the matched cinderellas inorder to refreash the screen

                    List<FairyGodmother> MatchedGodMother = new List<FairyGodmother>();//get the cinderellas that have been matched from the DB
                    match.GetPairedGodMother(ref MatchedGodMother, ref MatchedCinderella);//show the godmother that has been matched to the cinderella
                    MatchMakingLogic Logic = new MatchMakingLogic();

                    int Cinderellacount = UnsortCinderella.Count;//loop control variable

                    //gets the difference between check-in and appointment time
                    foreach (CinderellaClass cinder in UnsortCinderella)
                    {
                        cinder.diffFromappTime = match.EarlyLateTime(cinder.appTime);
                    }

                    //sorts the cinderellas based on the difference between checkin and appointment time

                    Cinderella = match.SortCinderellas(ref UnsortCinderella);

                    for (int i = 0; i < Cinderellacount; i++)//makes the matches
                    {
                        Logic.MakeMatch(ref Cinderella, ref GodMother, ref MatchedCinderella, ref MatchedGodMother);
                    }
                    //create lists to hold the matched ids of the cinderellas and the god mothers
                    List<int> MatchedCinderellaID = new List<int>();
                    List<int> MatchedGodMotherID = new List<int>();
                    //fill each of the lists
                    foreach (CinderellaClass Cinder in MatchedCinderella)
                    {
                        MatchedCinderellaID.Add(Cinder.getCinderellaID());
                    }
                    foreach (FairyGodmother PersonalShopper in MatchedGodMother)
                    {
                        MatchedGodMotherID.Add(PersonalShopper.getFairyID());
                    }
                    if ((MatchedCinderellaID.Count > 0) && (MatchedGodMotherID.Count > 0))
                    {//update the matches to the database.
                        SQL_Queries queries = new SQL_Queries();
                        //loops through all the matches nd make the appropiate DB action
                        for (int i = 0; i < MatchedCinderellaID.Count; i++)
                        {
                            if (queries.FGCurrentStatus(MatchedGodMotherID[i]) == 4)
                            {
                                //update the list of cinderellas taht ahve been through matchmaking
                                oldCinderellas.Add(MatchedCinderellaID[i]);

                                //pair the cinderellas
                                queries.pairCinderella(MatchedCinderellaID[i], MatchedGodMotherID[i]);
                                //set the status of the cinderella and the fairy god mother
                                if (queries.CinderellaCurrentStatus(MatchedCinderellaID[i]) != 3)
                                {
                                    queries.setCinderellaStatus(MatchedCinderellaID[i].ToString(), 3);
                                }
                                if (queries.FGCurrentStatus(MatchedGodMotherID[i]) != 2)
                                {
                                    queries.setFGStatus(MatchedGodMotherID[i].ToString(), 2);
                                }
                            }

                        }
                    }
                    //clear the lists of ID's
                    MatchedGodMotherID.Clear();
                    MatchedCinderellaID.Clear();
                    //pause execution for 5 seconds
                    Thread.Sleep(5000);
                }

            }
            catch (Exception e)
            {
                //a last hope in order to possibly work around random crashes.
                Thread bug = new Thread(() => Main());
                bug.Start();
            }
        }