public ActionResult Upload(FundsUploadViewModel viewmodel) { try { if (ModelState.IsValid) { if (viewmodel.ExcelFile != null && viewmodel.ExcelFile.ContentLength > 0) { var fileName = Path.GetFileName(viewmodel.ExcelFile.FileName); var path = Path.Combine(Server.MapPath("~/Uploads/Funds/"), DateTime.Now.GetTimeStamp() + "_" + fileName); viewmodel.ExcelFile.SaveAs(path); // save a copy of the uploaded file. // convert the uploaded file into datatable, then add/update db entities. var dtDistFunds = ImportUtils.ImportXlsxToDataTable(viewmodel.ExcelFile.InputStream, true, 1, true); var dtGcFunds = ImportUtils.ImportXlsxToDataTable(viewmodel.ExcelFile.InputStream, true, 2, true); var distFundAddEntities = dtDistFunds.AsEnumerable().Select(row => new FundAddEntity() { Year = int.Parse(row["Year"].ToString()), FundNumber = row["Fund Number"].ToString(), DisplayName = row["Display Name"].ToString(), MCAG = row["MCAG"].ToString(), MapTo = row["Map to"].ToString(), IsActive = int.Parse(row["Is Active"].ToString()) == 1, DbSource = DbSource.DIST }); var gcFundAddEntities = dtGcFunds.AsEnumerable().Select(row => new FundAddEntity() { Year = int.Parse(row["Year"].ToString()), FundNumber = row["Fund Number"].ToString(), DisplayName = row["Display Name"].ToString(), MCAG = row["MCAG"].ToString(), MapTo = row["Map to"].ToString(), IsActive = int.Parse(row["Is Active"].ToString()) == 1, DbSource = DbSource.GC }); List <FundAddEntity> rejectedFunds; var uploadedFunds = distFundAddEntities.Union(gcFundAddEntities).ToList(); var validFunds = _fundService.AddUploadedFunds(viewmodel.FundsYear.Value, uploadedFunds, out rejectedFunds); Success($"<strong>{validFunds.Count}</strong> Funds have been successfully saved."); if (rejectedFunds.Any()) { Danger($"<strong>{rejectedFunds.Count}</strong> Funds have been skipped. Please make sure that they exist in GP Dynamics system."); } } } return(RedirectToAction("Index", new { year = viewmodel.FundsYear.Value })); } catch { Danger("An error happened while updating Funds. Please try again."); return(View(viewmodel)); } }