public ActionResponse Enroll(Enrollment aEnrollment) { ActionResponse response = new ActionResponse(); try { bool isAlreadyEnrolled = aStudentGateway.IsAlreadyEnrolled(aEnrollment); if (isAlreadyEnrolled) { response.Class = "danger"; response.Message = "This student is already enrolled in this course."; return response; } aStudentGateway.Enroll(aEnrollment); response.Class = "success"; response.Message = "Enrollment successfully completed."; } catch (SqlException ex) { response.Class = "warning"; response.Message = ex.Message; } return response; }
public ActionResult Enroll(Enrollment aEnrollment) { ViewBag.response = aStudentManager.Enroll(aEnrollment); ViewBag.Students = aStudentManager.GetStudentSelectView(); return View(); }
public void Enroll(Enrollment aEnrollment) { string query = "INSERT INTO Enrollments (StudentId, CourseId, EnrollmentDate, IsCurrent) VALUES (@stdId, @courseId, @enrollmentDate, @isCurrent)"; using (connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.Clear(); command.Parameters.Add("stdId", sqlDbType: SqlDbType.Int); command.Parameters["stdId"].Value = aEnrollment.StudentId; command.Parameters.Add("courseId", sqlDbType: SqlDbType.Int); command.Parameters["courseId"].Value = aEnrollment.CourseId; command.Parameters.Add("enrollmentDate", sqlDbType: SqlDbType.Date); command.Parameters["enrollmentDate"].Value = aEnrollment.EnrollmentDate.Date; command.Parameters.Add("isCurrent", sqlDbType: SqlDbType.Bit); command.Parameters["isCurrent"].Value = aEnrollment.IsCurrent; connection.Open(); command.ExecuteNonQuery(); } }
public bool IsAlreadyEnrolled(Enrollment aEnrollment) { string query = "SELECT * FROM Enrollments WHERE StudentId =@stdId AND CourseId =@courseId AND IsCurrent =1"; using (connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.Clear(); command.Parameters.Add("stdId", sqlDbType: SqlDbType.Int); command.Parameters["stdId"].Value = aEnrollment.StudentId; command.Parameters.Add("courseId", sqlDbType: SqlDbType.Int); command.Parameters["courseId"].Value = aEnrollment.CourseId; connection.Open(); SqlDataReader reader = command.ExecuteReader(); return reader.HasRows; } }