public async Task <IActionResult> Create(EstudentViewModel model) { if (ModelState.IsValid) { var student = new Estudents { NOrden = model.NOrden, Document = model.Document, DeliveryActas = new List <DeliveryActa>(), FullName = model.FullName, AcudienteName = model.AcudienteName, DocumentAcu = model.DocumentAcu, Jornada = model.Jornada, Mesas = model.Mesa, Sedes = await _context.Sedes.FindAsync(model.SedeId), FechaActualización = DateTime.Now, AutDelivery = model.AutoDelivery }; try { _context.Add(student); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception e) { ViewBag.Message = $"Excepcion no Controlada: {e.Message} mas detalles:{e.InnerException}"; return(View(model)); } } return(View(model)); }
//[HttpPost] //public async Task<IActionResult> Index(int? id) //{ // if (id == null) // { // ViewBag.Message = $"Seleccione un Documento de Excel"; // } // try // { // } // catch (Exception) // { // throw; // } //} public async Task <IActionResult> GetStudentList(int?id) { if (id == null) { return(NotFound()); } var dateup = await _context.Sincros .FirstOrDefaultAsync(o => o.Id == id.Value); if (dateup == null) { return(NotFound()); } string fullUpdate = dateup.EndUpdate; try { var fileName = cn.StudentsServer(fullUpdate); if (fileName.Tables[0].Rows.Count > 0) { int contadorSave = 0; int contadorUpdate = 0; var studentIds = _context.Estudents.Select(t => t.Document).ToList(); List <Estudents> newEstudents = new List <Estudents>(); foreach (DataRow dr in fileName.Tables[0].Rows) { string nOder = dr["NOrden"].ToString(); string doc = dr["Document"].ToString(); string fullName = dr["FullName"].ToString(); string acudienteName = dr["AcudienteName"].ToString(); string documentAcu = dr["DocumentAcu"].ToString(); string mesas = dr["Mesas"].ToString(); string jornada = dr["Jornada"].ToString(); string autDelivery = dr["AutDelivery"].ToString(); string sedes = dr["SedesId"].ToString(); string dateUpdate = dr["DateUpdate"].ToString(); bool exits = studentIds.Any(t => t == doc); if (!exits) { Estudents newEstudent = new Estudents() { NOrden = nOder, FullName = fullName, Document = doc, AcudienteName = acudienteName, DocumentAcu = documentAcu, Sedes = await _context.Sedes.FindAsync(Convert.ToInt32(sedes)), Mesas = mesas, AutDelivery = autDelivery, Jornada = jornada, FechaActualización = Convert.ToDateTime(dateUpdate) }; newEstudents.Add(newEstudent); contadorSave++; } else { Estudents updatedEstudent = await _context.Estudents.SingleAsync(t => t.Document == doc); if (Convert.ToDateTime(dateUpdate) > updatedEstudent.FechaActualización) { updatedEstudent.NOrden = $"{updatedEstudent.NOrden}, {nOder}"; updatedEstudent.Document = updatedEstudent.Document; updatedEstudent.Sedes = await _context.Sedes.FindAsync(Convert.ToInt32(sedes)); updatedEstudent.FullName = updatedEstudent.FullName; updatedEstudent.AcudienteName = updatedEstudent.AcudienteName; updatedEstudent.DocumentAcu = updatedEstudent.DocumentAcu; updatedEstudent.AutDelivery = $"{updatedEstudent.AutDelivery}, {autDelivery}"; updatedEstudent.Mesas = $"{updatedEstudent.Mesas}, {mesas}"; updatedEstudent.Jornada = updatedEstudent.Jornada; updatedEstudent.FechaActualización = Convert.ToDateTime(dateUpdate); contadorUpdate++; _context.Estudents.Update(updatedEstudent); } } } if (newEstudents.Any()) { _context.Estudents.AddRange(newEstudents); } await _context.SaveChangesAsync(); ViewBag.Message = $"Se Encontraron {fileName.Tables[0].Rows.Count} Registros de los cuales {contadorSave} son Nuevos y {contadorUpdate} se actualizaron."; } else { ViewBag.Message = $"No hay Registros Nuevos"; } return(RedirectToAction($"Index")); } catch (Exception e) { ViewBag.Message = $"Exeption: {e.Message}"; return(RedirectToAction($"Index")); } }
public async Task <IActionResult> PostStudentList(int?id) { if (id == null) { return(NotFound()); } var dateup = await _context.Sincros .FirstOrDefaultAsync(o => o.Id == id.Value); if (dateup == null) { return(NotFound()); } DateTime fullUpdate = Convert.ToDateTime(dateup.EndUpdate); try { var fileName = _context.Estudents .Where(s => s.FechaActualización > fullUpdate) .ToList(); if (fileName.Count > 0) { int contadorSave = 0; int contadorUpdate = 0; var studentIds = cn.StudentsServer(fullUpdate.ToString()); DataRow[] foundRows; List <Estudents> newEstudents = new List <Estudents>(); foreach (var drs in fileName) { //Muestras los valores obteniendolos con el Índice o el Nombre de la columna, // de la siguiente manera: string rnOder = drs.NOrden; string rdoc = drs.Document; string rfullName = drs.FullName; string racudienteName = drs.AcudienteName; string rdocumentAcu = drs.DocumentAcu; string rmesas = drs.Mesas; string rjornada = drs.Jornada; string rautDelivery = drs.AutDelivery; string rsedes = Convert.ToString(drs.Sedes); DateTime rdateUpdate = drs.FechaActualización; foundRows = studentIds.Tables[0].Select($"Document = '{rdoc}'"); if (foundRows == null) { Estudents newEstudent = new Estudents() { NOrden = rnOder, FullName = rfullName, Document = rdoc, AcudienteName = racudienteName, DocumentAcu = rdocumentAcu, Sedes = await _context.Sedes.FindAsync(Convert.ToInt32(rsedes)), Mesas = rmesas, AutDelivery = rautDelivery, Jornada = rjornada, FechaActualización = Convert.ToDateTime(rdateUpdate) }; newEstudents.Add(newEstudent); contadorSave++; } else { Estudents updatedEstudent = await _context.Estudents.SingleAsync(t => t.Document == rdoc); if (Convert.ToDateTime(rdateUpdate) > updatedEstudent.FechaActualización) { updatedEstudent.NOrden = $"{updatedEstudent.NOrden}, {rnOder}"; updatedEstudent.Document = updatedEstudent.Document; updatedEstudent.Sedes = await _context.Sedes.FindAsync(Convert.ToInt32(rsedes)); updatedEstudent.FullName = updatedEstudent.FullName; updatedEstudent.AcudienteName = updatedEstudent.AcudienteName; updatedEstudent.DocumentAcu = updatedEstudent.DocumentAcu; updatedEstudent.AutDelivery = $"{updatedEstudent.AutDelivery}, {rautDelivery}"; updatedEstudent.Mesas = $"{updatedEstudent.Mesas}, {rmesas}"; updatedEstudent.Jornada = updatedEstudent.Jornada; updatedEstudent.FechaActualización = Convert.ToDateTime(rdateUpdate); contadorUpdate++; _context.Estudents.Update(updatedEstudent); } } } if (newEstudents.Any()) { _context.Estudents.AddRange(newEstudents); } await _context.SaveChangesAsync(); //ViewBag.Message = $"Se Encontraron {fileName.Tables[0].Rows.Count} Registros de los cuales {contadorSave} son Nuevos y {contadorUpdate} se actualizaron."; } else { ViewBag.Message = $"No hay Registros Nuevos"; } return(RedirectToAction($"Index")); } catch (Exception e) { string mensaje = "<script type='text/javascript'>alert('{0}')</script>"; mensaje = string.Format(mensaje, $"Hola: {e.Message}"); ViewBag.Message = $"Exeption: {e.Message}"; return(RedirectToAction($"Index")); } }
private async Task <Estudents> GetStudentList(string fName) { Estudents students = new Estudents(); try { var fileName = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\files"}" + "\\" + fName; System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); using (var stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read)) { using (var reader = ExcelReaderFactory.CreateReader(stream)) { int contadorSave = 0; int contadorUpdate = 0; while (reader.Read()) { string autorized = reader.GetValue(7).ToString(); string mesa = reader.GetValue(6).ToString(); string nOder = reader.GetValue(0).ToString(); string doc = reader.GetValue(2).ToString(); var exits = await _dataContext.Estudents .Include(d => d.Sedes) .FirstOrDefaultAsync(s => s.Document == doc); if (exits == null) { _dataContext.Estudents.Add(new Estudents() { NOrden = reader.GetValue(0).ToString(), FullName = reader.GetValue(1).ToString(), Document = reader.GetValue(2).ToString(), AcudienteName = reader.GetValue(3).ToString(), DocumentAcu = reader.GetValue(4).ToString(), Sedes = await _dataContext.Sedes.FirstAsync(o => o.NameSedes == reader.GetValue(5).ToString()), Mesas = mesa, AutDelivery = autorized, Jornada = reader.GetValue(8).ToString() //Site = _dataContext.Sites.FirstAsync(s => s.Id == (Convert.ToInt32(reader.GetValue(2).ToString()))) }); contadorSave++; } else { exits.NOrden = $"{exits.NOrden}, {nOder}"; exits.Document = exits.Document; exits.sedes2 = $"{exits.sedes2},{reader.GetValue(5).ToString()}"; exits.Sedes = await _dataContext.Sedes.FirstAsync(o => o.NameSedes == reader.GetValue(5).ToString()); exits.FullName = exits.FullName; exits.AcudienteName = exits.AcudienteName; exits.DocumentAcu = exits.DocumentAcu; exits.AutDelivery = $"{exits.AutDelivery}, {autorized}"; exits.Mesas = $"{exits.Mesas}, {mesa}"; exits.Jornada = exits.Jornada; contadorUpdate++; _dataContext.Estudents.Update(exits); } await _dataContext.SaveChangesAsync(); } ViewBag.Success = $"Se Encontraron {reader.RowCount} Registros de los cuales {contadorSave} son Nuevos y {contadorUpdate} se actualizaron."; } } return(students); } catch (Exception e) { ViewBag.Message = $"Excepcion no Controlada: {e.Message} mas detalles:{e.InnerException}"; } return(students); }