public ActionResult Add(HttpPostedFileBase file) { try { if (file.ContentLength > 0) { Stream fs = file.InputStream; ExcelPackage package = new ExcelPackage(fs); foreach (ExcelWorksheet worksheet in package.Workbook.Worksheets) { Class @class = new Models.Class(); for (int i = worksheet.Dimension.Start.Row + 1; i <= worksheet.Dimension.End.Row; i++) { if (Int32.Parse(worksheet.Cells[i, 1].Value.ToString()) != 0) { @class.CRN = Int32.Parse(worksheet.Cells[i, 1].Value.ToString()); @class.DeptPrefix = worksheet.Cells[i, 2].Value.ToString(); @class.ClassNum = worksheet.Cells[i, 3].Value.ToString(); @class.Time = worksheet.Cells[i, 4].Value.ToString(); @class.Days = worksheet.Cells[i, 5].Value.ToString(); @class.Instructor = worksheet.Cells[i, 6].Value.ToString(); db.Classes.Add(@class); db.SaveChanges(); } } } return(RedirectToAction("Class")); } else { ViewBag.Error = "The file you uploaded has no data in it, please upload another one."; //return error message return(View()); } } catch (Exception) { ViewBag.Error = "The data you inputted was not added to the database, please try again."; //return error message return(View()); } }
public ActionResult Name(PersonWeek pWeek) { //Create empty student to be used later. Student student = null; //Check if Student is already in DB. if (db.Students.Find(pWeek.VNum) != null) { student = db.Students.Find(pWeek.VNum); student.FirstName = pWeek.FirstName; student.LastName = pWeek.LastName; } //Create a student and set the class to the "placeholder class" created in the up script. else { //Add the student to the database. student = new Student { VNum = pWeek.VNum, FirstName = pWeek.FirstName, LastName = pWeek.LastName }; db.Students.Add(student); } try { //Save the changes to the database. db.SaveChanges(); } catch (Exception) { ViewBag.Error = "There was an error adding you to the database. Please ask a tutor for help."; return(View(pWeek)); } //Redirect to the select department method and slowly select the class. return(RedirectToAction("SelectClass", new { pWeek.Week, pWeek.VNum })); }
public JsonResult GetTimes(string id, string num, string instructor) { if (instructor == "Community College") { Class PCC = db.Classes .Where(c => c.DeptPrefix == id) .Where(d => d.ClassNum == num) .Where(n => n.Instructor == "Portland") .Select(p => p).FirstOrDefault(); Class ChCC = db.Classes .Where(c => c.DeptPrefix == id) .Where(d => d.ClassNum == num) .Where(n => n.Instructor == "Chemeketa") .Select(p => p).FirstOrDefault(); Class ClCC = db.Classes .Where(c => c.DeptPrefix == id) .Where(d => d.ClassNum == num) .Where(n => n.Instructor == "Clackamas") .Select(p => p).FirstOrDefault(); Class MHCC = db.Classes .Where(c => c.DeptPrefix == id) .Where(d => d.ClassNum == num) .Where(n => n.Instructor == "Mt. Hood") .Select(p => p).FirstOrDefault(); Class LBCC = db.Classes .Where(c => c.DeptPrefix == id) .Where(d => d.ClassNum == num) .Where(n => n.Instructor == "Linn-Benton") .Select(p => p).FirstOrDefault(); if (PCC == null || ChCC == null || ClCC == null || MHCC == null || LBCC == null) { if (PCC == null) { PCC = new Class { DeptPrefix = id, ClassNum = num, Instructor = "Portland" }; db.Classes.Add(PCC); } if (ChCC == null) { ChCC = new Class { DeptPrefix = id, ClassNum = num, Instructor = "Chemeketa" }; db.Classes.Add(ChCC); } if (ClCC == null) { ClCC = new Class { DeptPrefix = id, ClassNum = num, Instructor = "Clackamas" }; db.Classes.Add(ClCC); } if (MHCC == null) { MHCC = new Class { DeptPrefix = id, ClassNum = num, Instructor = "Mt. Hood" }; db.Classes.Add(MHCC); } if (LBCC == null) { LBCC = new Class { DeptPrefix = id, ClassNum = num, Instructor = "Linn-Benton" }; db.Classes.Add(LBCC); } try { db.SaveChanges(); } catch (Exception) { //There was an error. } } List <Class> CCList = new List <Class> { PCC, ChCC, ClCC, MHCC, LBCC }; //Convert the list into a Json Object string resultCC = JsonConvert.SerializeObject(CCList, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); //Return the Json Object return(Json(resultCC, JsonRequestBehavior.AllowGet)); } //Find all possible start times var startTimes = db.Classes .Where(c => c.DeptPrefix == id) .Where(d => d.ClassNum == num) .Where(n => n.Instructor == instructor) .Select(p => p) .ToList(); //Convert the list into a Json Object string result = JsonConvert.SerializeObject(startTimes, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); //Return the Json Object return(Json(result, JsonRequestBehavior.AllowGet)); }