string CheckAvail() { ArrayList list2 = new ArrayList(); list2.Add("1"); list2.Add("2"); list2.Add("3"); list2.Add("4"); list2.Add("5"); list2.Add("6"); list2.Add("Amazon"); list2.Add("Seminar Room"); ArrayList tempList = new ArrayList(); if (con.State == ConnectionState.Closed) { con.Open(); } { for (int p = 0; p < list2.Count; p++) { Console.WriteLine(p); // SqlDataAdapter sqlDa = new SqlDataAdapter("CheckAvailabity", con); string FirstNameQuery = "select dtstart,dtend,Date from BookingDetails where Lab ='" + list2[p] + "';"; // string FirstNameQuery = "select dtstart,dtend,Date from BookingDetails where Lab =" + DropDownList3.Text.Trim() +" And Date="+TextBox1.Text.Trim()+";"; string etime = "select dtend from BookingDetails where Lab ='" + Labss.Text.Trim() + "';"; string edatey = "select Date from BookingDetails where Lab ='" + Labss.Text.Trim() + "';"; SqlCommand FNAmeCom = new SqlCommand(FirstNameQuery, con); SqlCommand edateCom = new SqlCommand(etime, con); SqlCommand edateyCom = new SqlCommand(edatey, con); ClassName[] allRecords = null; ArrayList time = new ArrayList(); using (var command = new SqlCommand(FirstNameQuery, con)) { using (var reader = command.ExecuteReader()) { var list = new List <ClassName>(); while (reader.Read()) { list.Add(new ClassName("" + reader.GetTimeSpan(0), "" + reader.GetTimeSpan(1), "" + reader.GetDateTime(2))); } allRecords = list.ToArray(); reader.Close(); } } Boolean available = true; for (int i = 0; i < allRecords.Length; i++) { ClassName cur = new ClassName(DropDownList3.Text.Trim(), DropDownList4.Text.Trim(), startddate.Text.Trim()); // allRecords[i].sTime. if (cur.overlap(allRecords[i])) { available = false; } } if (available) { tempList.Add(list2[p]); } } string ToSend = ""; for (int k = 0; k < tempList.Count; k++) { ToSend += tempList[k]; ToSend += ", "; // ToSend += "Available labs"; } DataTable dtbl = new DataTable(); con.Close(); return(ToSend); } }