/// <summary> /// This method returns the Classroom object filled with information from /// the inputs in this control so that the content page can interact with it /// </summary> /// <returns>The filled Classroom object if validation succeeds, null otherwise</returns> public Models.Classroom GetClassroom() { if (ASPxEdit.AreEditorsValid(this, ValidationGroup)) { //Get the classroom pk int classroomPK = Convert.ToInt32(hfClassroomPK.Value); //Get the program fk int programFK = Convert.ToInt32(hfProgramFK.Value); //To hold the classroom object Models.Classroom classroom; //Determine if the classroom already exists if (classroomPK > 0) { using (PyramidContext context = new PyramidContext()) { classroom = context.Classroom.AsNoTracking().Where(c => c.ClassroomPK == classroomPK).FirstOrDefault(); } } else { classroom = new Models.Classroom(); } //Set the values from the inputs classroom.Name = txtName.Value.ToString(); classroom.ProgramSpecificID = txtProgramID.Value.ToString(); classroom.Location = (txtLocation.Value == null ? null : txtLocation.Value.ToString()); classroom.IsInfantToddler = Convert.ToBoolean(ddInfantToddler.Value); classroom.IsPreschool = Convert.ToBoolean(ddPreschool.Value); classroom.BeingServedSubstitute = Convert.ToBoolean(ddServedSubstitute.Value); classroom.ProgramFK = programFK; //Return the object return(classroom); } else { if (String.IsNullOrWhiteSpace(ValidationMessageToDisplay)) { ValidationMessageToDisplay = "Validation failed, see above for details!"; } return(null); } }
/// <summary> /// This method takes a primary key and it fills the inputs in the /// control with the information in the Classroom table for that primary key /// </summary> /// <param name="classroomPK">The primary key of the Classroom record</param> /// <param name="programFK">The primary key for the program which this classroom will be in</param> /// <param name="readOnly">True if the user is only allowed to view the values</param> public void InitializeWithData(int classroomPK, int programFK, bool readOnly) { using (PyramidContext context = new PyramidContext()) { //Set the program fk hidden field hfProgramFK.Value = programFK.ToString(); //Fill the used IDs hidden field var usedIDs = context.Classroom .AsNoTracking() .Where(cp => cp.ProgramFK == programFK && cp.ClassroomPK != classroomPK) .OrderBy(cp => cp.ProgramSpecificID) .Select(cp => cp.ProgramSpecificID) .ToList(); hfUsedIDs.Value = string.Join(",", usedIDs); if (classroomPK > 0) { //If this is an edit, fill the form with the classroom's information //Get the object Models.Classroom classroom = context.Classroom.AsNoTracking().Where(c => c.ClassroomPK == classroomPK).FirstOrDefault(); //Fill the hidden fields hfClassroomPK.Value = classroom.ClassroomPK.ToString(); //Fill the input fields txtName.Value = classroom.Name; txtProgramID.Value = classroom.ProgramSpecificID; txtLocation.Value = classroom.Location; ddInfantToddler.SelectedItem = ddInfantToddler.Items.FindByValue(classroom.IsInfantToddler); ddPreschool.SelectedItem = ddPreschool.Items.FindByValue(classroom.IsPreschool); ddServedSubstitute.SelectedItem = ddServedSubstitute.Items.FindByValue(classroom.BeingServedSubstitute); } //Set the controls usability txtName.ReadOnly = readOnly; txtProgramID.ReadOnly = readOnly; txtLocation.ReadOnly = readOnly; ddInfantToddler.ReadOnly = readOnly; ddPreschool.ReadOnly = readOnly; ddServedSubstitute.ReadOnly = readOnly; } }
public List <Models.Classroom> GetAllClassrooms() { Qurey = "SELECT * FROM classroom"; Command = new SqlCommand(Qurey, Connection); List <Models.Classroom> classrooms = new List <Models.Classroom>(); Connection.Open(); Reader = Command.ExecuteReader(); while (Reader.Read()) { Models.Classroom classroom = new Models.Classroom(); classroom.Id = Convert.ToInt32(Reader["id"]); classroom.RoomNo = Reader["roomNo"].ToString(); classrooms.Add(classroom); } Reader.Close(); Connection.Close(); return(classrooms); }
/// <summary> /// This method executes when the user clicks the delete button for a classroom /// and it deletes the classroom information from the database /// </summary> /// <param name="sender">The lbDeleteClassroom LinkButton</param> /// <param name="e">The Click event</param> protected void lbDeleteClassroom_Click(object sender, EventArgs e) { if (currentProgramRole.AllowedToEdit.Value) { //Get the PK from the hidden field int?removeClassroomPK = (String.IsNullOrWhiteSpace(hfDeleteClassroomPK.Value) ? (int?)null : Convert.ToInt32(hfDeleteClassroomPK.Value)); //Remove the role if the PK is not null if (removeClassroomPK != null) { try { using (PyramidContext context = new PyramidContext()) { //Get the classroom program row to remove Models.Classroom classroomToRemove = context.Classroom.Find(removeClassroomPK); //Remove the classroom context.Classroom.Remove(classroomToRemove); context.SaveChanges(); //Show a success message msgSys.ShowMessageToUser("success", "Success", "Successfully deleted classroom!", 10000); } } catch (DbUpdateException dbUpdateEx) { //Check if it is a foreign key error if (dbUpdateEx.InnerException?.InnerException is SqlException) { //If it is a foreign key error, display a custom message SqlException sqlEx = (SqlException)dbUpdateEx.InnerException.InnerException; if (sqlEx.Number == 547) { msgSys.ShowMessageToUser("danger", "Error", "Could not delete the classroom, there are related records in the system!<br/><br/>If you do not know what related records exist, please contact tech support via ticket.", 120000); } else { msgSys.ShowMessageToUser("danger", "Error", "An error occurred while deleting the classroom!", 120000); } } else { msgSys.ShowMessageToUser("danger", "Error", "An error occurred while deleting the classroom!", 120000); } //Log the error Elmah.ErrorSignal.FromCurrentContext().Raise(dbUpdateEx); } //Rebind the classroom controls bsGRClassrooms.DataBind(); //Bind the chart BindClassroomChart(); } else { msgSys.ShowMessageToUser("danger", "Error", "Could not find the classroom to delete!", 120000); } } else { msgSys.ShowMessageToUser("danger", "Error", "You are not authorized to make changes!", 120000); } }