protected void Button_AssignDesks_Click(object sender, EventArgs e)
        {
            string ErrorS = "Success!";

            bool     Is_AM = true;
            DateTime t0    = new DateTime();

            Is_AM = (bool)ViewState["Session_is_AM"];
            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);
            //going to assign desks for this session
            //first clear all desks.....
            ExamsUtility u = new ExamsUtility();

            u.ClearDeskAssignments(t1, t2);

            ScheduledComponentList scl1 = new ScheduledComponentList();

            scl1.LoadList_orderbyRoom(t1, t2);

            Guid room1 = new Guid(); room1 = Guid.Empty;

            foreach (ScheduledComponent sc in scl1.m_List)
            {
                if (sc.m_RoomId != room1)
                {
                    if (!AllocateDesksRoom(sc.m_RoomId, t1, t2, ref ErrorS))
                    {
                        Label_message.Text    = ErrorS;
                        Label_message.Visible = true;
                        return;
                    }
                    room1 = sc.m_RoomId;
                }
            }
            scl1.LoadList(t1, t2);
            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;
        }