protected void SetUp(DateTime t1, DateTime t2, bool Is_AM)
        {
            DateTime t3 = new DateTime();
            ScheduledComponentList scl1 = new ScheduledComponentList();

            scl1.LoadList_Date(t1, t2);//order by  DateTime
            t3 = new DateTime(2000, 1, 1); int nmax = 0; int n = 0;
            Guid st1 = new Guid(); st1 = Guid.Empty;

            foreach (ScheduledComponent sc in scl1.m_List)
            {
                if (t3 != sc.m_Date)
                {
                    t3   = sc.m_Date;
                    nmax = (n > nmax) ? n : nmax;
                    n    = 0;
                }
                n++;
            }
            nmax = (n > nmax) ? n : nmax;
            //so max kids nmax //offer rooms

            Label_RoomsAllcation.Text     = "Allocated: 0    Needed: " + nmax.ToString();
            ViewState["MaxDesksRequired"] = nmax;
            //setup list box
            ListBox_Rooms.Items.Clear();
            string s = "SELECT DISTINCT  dbo.tbl_Core_Rooms.RoomId AS Id, dbo.tbl_Core_Rooms.RoomCode AS Name ";

            s += " FROM  dbo.tbl_Core_Rooms INNER JOIN dbo.tbl_Exams_RoomLayouts ON dbo.tbl_Core_Rooms.RoomId = dbo.tbl_Exams_RoomLayouts.RoomId ";
            s += " ORDER BY dbo.tbl_Core_Rooms.RoomCode ";
            Encode en = new Encode();

            SqlDataSource2.SelectCommand    = s;
            SqlDataSource2.ConnectionString = en.GetDbConnection();
            SqlDataSource2.DataBind();

            foreach (ListItem l in ListBox_Rooms.Items)
            {
                if (l.Text == "HAL")
                {
                    s = s;
                }
            }

            SetupRulesBoxesSources(t1, t2, Is_AM);
        }
        private void SetupDeskGrid(DateTime t1, DateTime t2)
        {
            System.Drawing.Color[] cell_colours = GetColors();

            Label_DeskDate.Text = t1.ToLongDateString();
            //setup dropdown list of rooms...
            List <Guid> Rooms = new List <Guid>();


            //need to load all rooms used...


            string s = "SELECT DISTINCT RoomId FROM  dbo.tbl_Exams_ScheduledComponents ";

            s += " WHERE (DateTime > CONVERT(DATETIME,'" + t1.ToString("yyyy-MM-dd HH:mm:ss") + "',102) ) ";
            s += " AND (DateTime < CONVERT(DATETIME,'" + t2.ToString("yyyy-MM-dd HH:mm:ss") + "',102) ) ";
            Encode en = new Encode();

            using (SqlConnection cn = new SqlConnection(en.GetDbConnection()))
            {
                cn.Open();
                using (SqlCommand cm = new SqlCommand(s, cn))
                {
                    using (SqlDataReader dr = cm.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (dr.IsDBNull(0))
                            {
                                Rooms.Add(Guid.Empty);
                            }
                            else
                            {
                                Rooms.Add(dr.GetGuid(0));
                            }
                        }
                        dr.Close();
                    }
                }
                cn.Close();
            }

            if ((Rooms.Count == 1) && (Rooms[0] == Guid.Empty))
            {
                Label_message.Text    = " No Rooms Allocated...";
                Label_message.Visible = true;
            }

            DropDownListDeskRooms.Items.Clear();
            SimpleRoom r1 = new SimpleRoom();

            foreach (Guid d in Rooms)
            {
                r1.Load(d.ToString());
                ListItem l = new ListItem(r1.m_roomcode, d.ToString());
                DropDownListDeskRooms.Items.Add(l);
            }


            //need to find all the changes...
            DateTime t0 = new DateTime(); t0 = t1;
            ScheduledComponentList scl1 = new ScheduledComponentList(); scl1.LoadList_Date(t1, t2);

            while (GoToNextChange(ref t0, scl1, Rooms[0]))
            {
                ListItem l = new ListItem(t0.AddMinutes(-1).ToShortTimeString(), t0.ToString());
                DropDownListDeskTimes.Items.Add(l);
            }

            UpdateDeskGrid();
        }
        protected void Button_Assign_Click(object sender, EventArgs e)
        {
            //now we are actually going to do it???

            string   ErrorS = "Success!";
            bool     Is_AM  = (bool)ViewState["Session_is_AM"];
            DateTime t0     = new DateTime();

            t0 = (DateTime)ViewState["EditDate"];
            DateTime t1 = new DateTime();
            DateTime t2 = new DateTime();

            t1 = (Is_AM) ? t0.AddHours(8) : t0.AddHours(13);
            t2 = (Is_AM) ? t0.AddHours(13) : t0.AddHours(18);
            DateTime time_last = new DateTime();



            List <Guid> Rooms = new List <Guid>();
            ListBox     lb    = ListBox_Rooms;

            foreach (ListItem l in lb.Items)
            {
                if (l.Selected)
                {
                    Rooms.Add(new Guid(l.Value));
                }
            }

            //if no rooms allocated flag error
            if (Rooms.Count == 0)
            {
                ErrorS = "...you haven't allocated Any rooms";
                goto ErrorExit;
            }


            ExamsUtility eu = new ExamsUtility();

            eu.ClearDeskAssignments(t1, t2);

            //ought to clear all room allocations first...
            //ought to do this by query
            int          n = 0;
            ExamsUtility u = new ExamsUtility();

            u.ClearRoomAssignments(t1, t2);

            ExamRoom er = new ExamRoom();

            n = 0;
            List <int> Room_Capacity = new List <int>();

            foreach (Guid d in Rooms)
            {
                er.Load(d);
                Room_Capacity.Add(er.m_capacity);
                n++;
            }
            int nmax = n - 1;

            ScheduledComponentList scl1 = new ScheduledComponentList();

            scl1.LoadList_Date(t1, t2);                              //order by  DateTime
            time_last = ((ScheduledComponent)scl1.m_List[0]).m_Date;
            string s = GetQueryStringDay(t0.Year, t0.Month, t0.Day); // gets from sc ordered by datetime,NUMBER

            List <Guid>     Components    = new List <Guid>();
            List <int>      number        = new List <int>();
            List <DateTime> Start_Time    = new List <DateTime>();
            Encode          en            = new Encode();
            string          db_connection = en.GetDbConnection();

            using (SqlConnection cn = new SqlConnection(db_connection))
            {
                cn.Open();
                using (SqlCommand cm = new SqlCommand(s, cn))
                {
                    using (SqlDataReader dr = cm.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            Components.Add(dr.GetGuid(1));
                            number.Add(dr.GetInt32(4));
                            Start_Time.Add(dr.GetDateTime(3));
                        }
                        dr.Close();
                    }
                }
                cn.Close();
            }

            //first apply rules... hopefully these will fit ... if not flag error back to ui

            int CurrentRoomCapacity = 0;

            string s_rule      = DropDownList_Rule1Source.SelectedItem.Text;
            Guid   rule_roomId = new Guid(DropDownList_Rule1Room.SelectedValue);

            n = 0; foreach (Guid d in Rooms)
            {
                if (d == rule_roomId)
                {
                    CurrentRoomCapacity = Room_Capacity[n]; break;
                }
            }
            if (!Apply_Rule(s_rule, rule_roomId, scl1, ref ErrorS, Components, number, ref CurrentRoomCapacity))
            {
                goto ErrorExit;
            }
            Room_Capacity[n] = CurrentRoomCapacity;
            if ((s_rule != "None") && (CheckBox1.Checked))
            {
                Rooms.Remove(rule_roomId); nmax--;
            }


            s_rule      = DropDownList_Rule2Source.SelectedItem.Text;
            rule_roomId = new Guid(DropDownList_Rule2Room.SelectedValue);
            n           = 0; foreach (Guid d in Rooms)
            {
                if (d == rule_roomId)
                {
                    CurrentRoomCapacity = Room_Capacity[n]; break;
                }
            }
            if (!Apply_Rule(s_rule, rule_roomId, scl1, ref ErrorS, Components, number, ref CurrentRoomCapacity))
            {
                goto ErrorExit;
            }
            if ((s_rule != "None") && (CheckBox2.Checked))
            {
                Rooms.Remove(rule_roomId); nmax--;
            }
            Room_Capacity[n] = CurrentRoomCapacity;


            s_rule      = DropDownList_Rule3Source.SelectedItem.Text;
            rule_roomId = new Guid(DropDownList_Rule3Room.SelectedValue);
            n           = 0; foreach (Guid d in Rooms)
            {
                if (d == rule_roomId)
                {
                    CurrentRoomCapacity = Room_Capacity[n]; break;
                }
            }
            if (!Apply_Rule(s_rule, rule_roomId, scl1, ref ErrorS, Components, number, ref CurrentRoomCapacity))
            {
                goto ErrorExit;
            }
            if ((s_rule != "None") && (CheckBox3.Checked))
            {
                Rooms.Remove(rule_roomId); nmax--;
            }
            Room_Capacity[n] = CurrentRoomCapacity;



            s_rule      = DropDownList_Rule4Source.SelectedItem.Text;
            rule_roomId = new Guid(DropDownList_Rule4Room.SelectedValue);
            n           = 0; foreach (Guid d in Rooms)
            {
                if (d == rule_roomId)
                {
                    CurrentRoomCapacity = Room_Capacity[n]; break;
                }
            }
            if (!Apply_Rule(s_rule, rule_roomId, scl1, ref ErrorS, Components, number, ref CurrentRoomCapacity))
            {
                goto ErrorExit;
            }
            if ((s_rule != "None") && (CheckBox4.Checked))
            {
                Rooms.Remove(rule_roomId); nmax--;
            }
            Room_Capacity[n] = CurrentRoomCapacity;



            s_rule      = DropDownList_Rule5Source.SelectedItem.Text;
            rule_roomId = new Guid(DropDownList_Rule5Room.SelectedValue);
            n           = 0; foreach (Guid d in Rooms)
            {
                if (d == rule_roomId)
                {
                    CurrentRoomCapacity = Room_Capacity[n]; break;
                }
            }
            if (!Apply_Rule(s_rule, rule_roomId, scl1, ref ErrorS, Components, number, ref CurrentRoomCapacity))
            {
                goto ErrorExit;
            }
            if ((s_rule != "None") && (CheckBox5.Checked))
            {
                Rooms.Remove(rule_roomId); nmax--;
            }
            Room_Capacity[n] = CurrentRoomCapacity;


            ScheduledComponentList scl2 = new ScheduledComponentList();

            foreach (ScheduledComponent c in scl1.m_List)
            {
                if (c.m_RoomId == Guid.Empty)
                {
                    scl2.m_List.Add(c);
                }
            }

            //now we apply the rest
            //idea is we find the sc with most students.... and put into biggest room first
            //first correct room capacity?
            n = 0;
            // well i think we have all the information....
            //going to assign biggest exam to biggest room first

            int index = 0;

            foreach (Guid d in Components)
            {
                //so new component....
                //if there is a complete gap before we start this component we can clear room allocations....
                //find the first (earliest) scheduled component
                foreach (ScheduledComponent c in scl2.m_List)
                {
                    if (c.m_ComponentId == d)
                    {
                        if (c.m_RoomId == Guid.Empty)
                        {
                            if (c.m_Date > time_last.AddMinutes(10))
                            {
                                //so this one starts at least 10 minutes after all sofar have finished....
                                time_last = c.m_Date.AddMinutes(c.m_TimeAllowed);
                                //so can reset rooms..... ie back to full capacity?
                                Room_Capacity.Clear(); n = 0;
                                foreach (Guid d1 in Rooms)
                                {
                                    er.Load(d1);
                                    Room_Capacity.Add(er.m_capacity);
                                    n++;
                                }
                                nmax = n - 1;
                            }
                        }
                    }
                }

                n = 0;
                while ((Room_Capacity[n] == 0) && (n <= nmax))
                {
                    n++;
                }
                if (n > nmax)
                {
                    ErrorS = "Ran out of room capacity !!"; goto ErrorExit;
                }
                //going to check if next one will fit in any room we have available... if so use it!  search smallest first?
                for (int i = nmax; i >= 0; i--)
                {
                    if (Room_Capacity[i] >= number[index])
                    {
                        n = i; break;
                    }
                }
                foreach (ScheduledComponent c in scl2.m_List)
                {
                    if (c.m_ComponentId == d)
                    {
                        if (c.m_RoomId == Guid.Empty)
                        {
                            if (c.m_Date.AddMinutes(c.m_TimeAllowed) > time_last)
                            {
                                time_last = c.m_Date.AddMinutes(c.m_TimeAllowed);
                            }

                            //really ought to check to see if he has any other componets in this session and assign the same room/...

                            foreach (ScheduledComponent c1 in scl1.m_List)
                            {
                                if (c1.m_StudentId == c.m_StudentId)
                                {
                                    c.m_RoomId = c1.m_RoomId;
                                }
                            }
                            if (c.m_RoomId == Guid.Empty)
                            {
                                c.m_RoomId = Rooms[n];
                                Room_Capacity[n]--;
                            }

                            c.Save();



                            foreach (ScheduledComponent c1 in scl2.m_List)
                            {
                                if ((c1.m_StudentId == c.m_StudentId) && (c1.m_RoomId == Guid.Empty))
                                {
                                    c.m_RoomId = Rooms[n];
                                    c.Save();
                                    //Room_Capacity[n]--;   should already have cleared clashes
                                }
                            }
                        }
                    }
                    while ((Room_Capacity[n] == 0) && (n < nmax))
                    {
                        n++;
                    }
                    if (n > nmax)
                    {
                        ErrorS = "Ran out of room capacity !!"; goto ErrorExit;
                    }
                }
                index++;
            }
            //check??
            //success;  open the summary panel

            Panel_left1.Visible             = false; Panel_right1.Visible = false;
            Label_message.Text              = "Rooms Allocated Successfully.";
            Label_message.Visible           = true;
            SqlDataSource3.SelectCommand    = GetQueryStringRoomSummary(t1, t2, true);
            SqlDataSource3.ConnectionString = en.GetDbConnection();
            SqlDataSource3.DataBind();
            return;

            ErrorExit : Label_message.Text = ErrorS;
            Label_message.BackColor        = System.Drawing.Color.Red;
            return;
        }