// GET: NewElectivesTables/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            NewElectivesTable newElectivesTable = db.NewElectivesTables.Find(id);

            if (newElectivesTable == null)
            {
                return(HttpNotFound());
            }

            ViewBag.SubCode = new SelectList(db.Subjects, "SubCode", "SubName", newElectivesTable.SubCode);
            if (!User.IsInRole("Admin"))
            {
                string department = (string)TempData.Peek("Department");
                ViewBag.USN = new SelectList(db.STUDENTs.Where(x => x.DeptID == department), "USN", "USN", newElectivesTable.SubCode);
            }
            else
            {
                ViewBag.USN = new SelectList(db.STUDENTs, "USN", "USN", newElectivesTable.USN);
            }
            return(View(newElectivesTable));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            NewElectivesTable newElectivesTable = db.NewElectivesTables.Find(id);

            db.NewElectivesTables.Remove(newElectivesTable);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        // GET: NewElectivesTables/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            NewElectivesTable newElectivesTable = db.NewElectivesTables.Find(id);

            if (newElectivesTable == null)
            {
                return(HttpNotFound());
            }
            return(View(newElectivesTable));
        }
 public ActionResult Edit([Bind(Include = "USN,SubCode,id")] NewElectivesTable newElectivesTable)
 {
     if (ModelState.IsValid)
     {
         db.Entry(newElectivesTable).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.SubCode = new SelectList(db.Subjects, "SubCode", "SubName", newElectivesTable.SubCode);
     if (!User.IsInRole("Admin"))
     {
         string department = (string)TempData.Peek("Department");
         ViewBag.USN = new SelectList(db.STUDENTs.Where(x => x.DeptID == department), "USN", "USN", newElectivesTable.SubCode);
     }
     else
     {
         ViewBag.USN = new SelectList(db.STUDENTs, "USN", "USN", newElectivesTable.USN);
     }
     return(View(newElectivesTable));
 }
        public ActionResult BulkUpload(HttpPostedFileBase postedFile)
        {
            string filePath = string.Empty;
            List <ElectiveError> errorList = new List <ElectiveError>();

            if (postedFile != null)
            {
                string path = Server.MapPath("~/Uploads/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                filePath = path + Path.GetFileName(postedFile.FileName) + DateTime.Now.Ticks;
                string extension = Path.GetExtension(postedFile.FileName);
                postedFile.SaveAs(filePath);

                Excel.Application xlApp       = new Excel.Application();
                Excel.Workbook    xlWorkbook  = xlApp.Workbooks.Open(filePath);
                Excel._Worksheet  xlWorksheet = xlWorkbook.Sheets[1];
                Excel.Range       xlRange     = xlWorksheet.UsedRange;

                int rowCount = xlRange.Rows.Count;

                for (int i = 2; i <= rowCount; i++)
                {
                    string Usn         = xlRange.Cells[i, 1].Value2.ToString();
                    string SubjectCode = xlRange.Cells[i, 2].Value2.ToString();

                    NewElectivesTable table = new NewElectivesTable
                    {
                        SubCode = SubjectCode,
                        USN     = Usn
                    };

                    try
                    {
                        db.NewElectivesTables.Add(table);
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException e)
                    {
                        ElectiveError error = new ElectiveError();
                        error.Usn         = Usn;
                        error.SubjectCode = SubjectCode;
                        foreach (var eve in e.EntityValidationErrors)
                        {
                            foreach (var ve in eve.ValidationErrors)
                            {
                                error.Errors.Add(new Error
                                {
                                    AttributeName = ve.PropertyName,
                                    ErrorMessage  = ve.ErrorMessage
                                });
                            }
                        }
                        errorList.Add(error);
                    }
                    catch (DbUpdateException d)
                    {
                        ElectiveError error = new ElectiveError();
                        error.Usn         = Usn;
                        error.SubjectCode = SubjectCode;
                        error.Errors.Add(new Error
                        {
                            AttributeName = "",
                            ErrorMessage  = "Invalid Combination"
                        });
                        errorList.Add(error);
                    }
                }
            }

            return(View("ElectivesError", errorList));
        }