//Get student by Id. Using IHttpActionResult public IHttpActionResult GetSudentById(int id) { StudentViewModel Student = null; using (var ctx = new SchoolEntities1()) { Student = ctx.Students.Include("StudentAddress").Include("Standard") .Where(s => s.StudentID == id) .Select(s => new StudentViewModel() { Id = s.StudentID, StudentName = s.StudentName, stardardId = s.StandardId ?? default(int), Standard = s.StandardId == null? null : new StandardViewModel() { StandardName = s.Standard.StandardName, Description = s.Standard.Description, }, Address = s.StudentAddress == null ? null : new AddressViewModel() { Id = s.StudentID, Address1 = s.StudentAddress.Address1, Address2 = s.StudentAddress.Address2, City = s.StudentAddress.City, State = s.StudentAddress.State, } }).FirstOrDefault <StudentViewModel>(); } if (Student == null) { return(NotFound()); } return(Ok(Student)); }
//Deletes student data from database public IHttpActionResult Delete(int id) { if (id <= 0) { return(BadRequest("Not a valid student Id")); } using (var ctx = new SchoolEntities1()) { var existingStudent = ctx.Students .Where(s => s.StudentID == id) .FirstOrDefault(); if (existingStudent != null) { ctx.Students.Remove(existingStudent); ctx.SaveChanges(); } else { return(NotFound()); } } return(Ok()); }
//Updates the existing data in the database public IHttpActionResult PutStudent(StudentViewModel studentAddress) { if (!ModelState.IsValid) { return(BadRequest("Invalid data")); } using (var ctx = new SchoolEntities1()) { var existingStudent = ctx.Students .Where(s => s.StudentID == studentAddress.Id) .FirstOrDefault(); if (existingStudent != null) { existingStudent.StudentName = studentAddress.StudentName; existingStudent.StandardId = studentAddress.stardardId; existingStudent.StudentAddress.Address1 = studentAddress.Address.Address1; existingStudent.StudentAddress.Address2 = studentAddress.Address.Address2; existingStudent.StudentAddress.City = studentAddress.Address.City; existingStudent.StudentAddress.State = studentAddress.Address.State; ctx.SaveChanges(); } else { NotFound(); } } return(Ok()); }
//Gets all the students. Method using IHttpActionResult. public IHttpActionResult GetAllStudents(bool includeAddress = false) { IList <StudentViewModel> Students = null; using (var ctx = new SchoolEntities1()) { Students = ctx.Students.Include("StudentAddress").Include("Standard") .Select(s => new StudentViewModel() { Id = s.StudentID, StudentName = s.StudentName, stardardId = s.StandardId ?? default(int), Standard = s.StandardId == null? null : new StandardViewModel() { StandardName = s.Standard.StandardName, Description = s.Standard.Description, }, Address = s.StudentAddress == null || includeAddress == false ? null : new AddressViewModel() { Id = s.StudentID, Address1 = s.StudentAddress.Address1, Address2 = s.StudentAddress.Address2, City = s.StudentAddress.City, State = s.StudentAddress.State, } }).ToList <StudentViewModel>(); } if (Students.Count == 0) { return(NotFound()); } return(Ok(Students)); }
//Posts new student in the datbase public IHttpActionResult PostNewStudent(StudentViewModel studentAddress) { //This will make sure that the student object includes all the necessary information. If it is not valid, it will return BadRequest response. if (!ModelState.IsValid) { return(BadRequest("Invalid data")); } using (var ctx = new SchoolEntities1()) { ctx.Students.Add(new Student() { StudentID = studentAddress.Id, StudentName = studentAddress.StudentName, StandardId = studentAddress.stardardId, }); ctx.StudentAddresses.Add(new StudentAddress() { StudentID = studentAddress.Address.Id, Address1 = studentAddress.Address.Address1, Address2 = studentAddress.Address.Address2, City = studentAddress.Address.City, State = studentAddress.Address.State, }); ctx.SaveChanges(); } return(Ok()); }
//Gets all the teachers public IHttpActionResult GetAllTeachers() { IList <Teacher> teachers = null; using (var ctx = new SchoolEntities1()) { teachers = ctx.Teachers.Select(t => t.) } }
public static IEnumerable <SelectListItem> getStandardList() { using (var ctx = new SchoolEntities1()) { var standardList = ctx.Standards. Select(s => new SelectListItem() { Value = s.StandardId.ToString(), Text = s.StandardName, }).ToList(); var standardSelct = new SelectListItem() { Value = null, Text = "---Select student standard---", }; standardList.Insert(0, standardSelct); return(standardList); } }
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string CourseName = ""; if (Request.QueryString["CourseName"] != null) { CourseName = Request.QueryString["CourseName"].ToString(); using (var db = new SchoolEntities1()) { //var courses = db.Course.ToList(); //.Where(t => t.FirstName.Contains(searchText) || t.LastName.Contains(searchText)).OrderBy(a => a.CustomerID).ToList(); var courses = db.Course.Where(t => t.Title.Contains(CourseName)).OrderBy(a => a.Title).ToList(); ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/Courses.rdlc"); ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.EnableHyperlinks = true; ReportViewer1.LocalReport.EnableExternalImages = true; ReportDataSource rpt = new ReportDataSource("Courses", courses); ReportParameter[] parameters = new ReportParameter[1]; parameters[0] = new ReportParameter("CourseName", CourseName); ReportViewer1.LocalReport.SetParameters(parameters); ReportViewer1.LocalReport.DataSources.Add(rpt); ReportViewer1.LocalReport.Refresh(); ReportViewer1.DataBind(); } } } }