private bool IsCorseCodeValid(Course aCourse) { if (aCourse.Code.Length > 5) { return true; } return false; }
public ActionResult Save(Course aCourse) { try { string message = courseManager.Save(aCourse); ViewBag.Mgs = message; List<Department> departments = departmentManager.GetAll().ToList(); List<Semester> semesters = semesterManager.GetAll.ToList(); ViewBag.Departments = departments; ViewBag.Semesters = semesters; return View(); //return RedirectToAction("Index"); } catch { return View(); } }
public string Save(Course aCourse) { if (!(IsCorseCodeValid(aCourse))) { return "Course must be at least 5 character of length"; } if (IsCourseCodeExits(aCourse.Code)) { return "Course code must be unique"; } if (IsCourseNameExits(aCourse.Name)) { return "Course Name must be unique"; } if (courseGateway.Insert(aCourse) > 0) { return "Saved Sucessfully"; } return "Failed to save"; }
public int Insert(Course aCourse) { try { CommandObj.CommandText = "spAddCourse"; CommandObj.CommandType = CommandType.StoredProcedure; CommandObj.Parameters.Clear(); CommandObj.Parameters.AddWithValue("@Code", aCourse.Code.ToUpper()); CommandObj.Parameters.AddWithValue("@Name", aCourse.Name); CommandObj.Parameters.AddWithValue("@Credit", aCourse.Credit); CommandObj.Parameters.AddWithValue("@Description", aCourse.Description); CommandObj.Parameters.AddWithValue("@DepartmentId", aCourse.DepartmentId); CommandObj.Parameters.AddWithValue("@SemesterId", aCourse.SemesterId); ConnectionObj.Open(); return CommandObj.ExecuteNonQuery(); } catch (Exception exception) { throw new Exception("Unable to Seve course", exception); } finally { ConnectionObj.Close(); CommandObj.Dispose(); } }
public IEnumerable<Course> GetCoursesTakeByaStudentByStudentId(int id) { try { List<Course> courses = new List<Course>(); CommandObj.CommandText = "spGetCoursesTakenByaStudent"; CommandObj.CommandType = CommandType.StoredProcedure; CommandObj.Parameters.Clear(); CommandObj.Parameters.AddWithValue("@StudentId", id); ConnectionObj.Open(); SqlDataReader reader = CommandObj.ExecuteReader(); while (reader.Read()) { Course aCourse = new Course { Id = Convert.ToInt32(reader["Id"].ToString()), Name = reader["Name"].ToString(), Code = reader["Code"].ToString(), Credit = Convert.ToDouble(reader["Credit"].ToString()), DepartmentId = Convert.ToInt32(reader["DepartmentId"].ToString()), Description = reader["Descirption"].ToString(), SemesterId = Convert.ToInt32(reader["SemesterId"].ToString()) }; courses.Add(aCourse); } reader.Close(); return courses; } catch (Exception exception) { throw new Exception("Unable to collect Courses",exception); } finally { ConnectionObj.Close(); CommandObj.Dispose(); } }
public Course GetCourseByName(string name) { try { string query = "SELECT * FROM t_Course WHERE Name=@name"; CommandObj.CommandText = query; CommandObj.Parameters.Clear(); CommandObj.Parameters.AddWithValue("@name", name); ConnectionObj.Open(); Course course = null; SqlDataReader reader = CommandObj.ExecuteReader(); if(reader.Read()) { course = new Course { Id = Convert.ToInt32(reader["Id"].ToString()), Name = reader["Name"].ToString(), Code = reader["Code"].ToString(), Credit = Convert.ToDouble(reader["Credit"].ToString()), Description = reader["Descirption"].ToString(), DepartmentId = Convert.ToInt32(reader["DepartmentId"].ToString()), SemesterId = Convert.ToInt32(reader["SemesterId"].ToString()) }; } reader.Close(); return course; } catch (Exception exception) { throw new Exception("Unable to collect course", exception); } finally { ConnectionObj.Close(); CommandObj.Dispose(); } }
public IEnumerable<Course> GetCourseByDepartmentId(int departmentId) { try { string query = "SELECT * FROM t_Course WHERE DepartmentId='"+departmentId+"'"; CommandObj.CommandText = query; List<Course> courses = new List<Course>(); ConnectionObj.Open(); SqlDataReader reader = CommandObj.ExecuteReader(); while (reader.Read()) { Course course = new Course { Id = Convert.ToInt32(reader["Id"].ToString()), Name = reader["Name"].ToString(), Code = reader["Code"].ToString(), Credit = Convert.ToDouble(reader["Credit"].ToString()), Description = reader["Descirption"].ToString(), DepartmentId = Convert.ToInt32(reader["DepartmentId"].ToString()), SemesterId = Convert.ToInt32(reader["SemesterId"].ToString()) }; courses.Add(course); } reader.Close(); return courses; } catch (Exception exception) { throw new Exception("Unable to collect courses", exception); } finally { ConnectionObj.Close(); CommandObj.Dispose(); } }