public TolPP(Context.TolPulang dbitem) { Id = dbitem.Id; IdTol = dbitem.IdTol; NamaTol = dbitem.JnsTol.NamaTol; IdRuteTol = dbitem.IdRuteTol; }
public string UploadRuteTol(IEnumerable <HttpPostedFileBase> filesRuteTol) { ResponeModel response = new ResponeModel(); if (filesRuteTol != null) { foreach (var file in filesRuteTol) { try { using (var package = new ExcelPackage(file.InputStream)) { var currentSheet = package.Workbook.Worksheets; var workSheet = currentSheet.First(); var noOfRow = workSheet.Dimension.End.Row; // sheet 1 for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++) { if (workSheet.Cells[rowIterator, 1].Value != null && workSheet.Cells[rowIterator, 2].Value != null) { int id = 0; Context.RuteTol dbitem = new Context.RuteTol(); if (workSheet.Cells[rowIterator, 5].Value != null) { int resId; if (int.TryParse(workSheet.Cells[rowIterator, 5].Value.ToString(), out resId)) { id = resId; } } if (id != 0) { dbitem = RepoRuteTol.FindByPK(id); dbitem.ListTolBerangkat.Clear(); dbitem.ListTolPulang.Clear(); } try { //parent dbitem.IdRute = RepoRute.FindByKode(workSheet.Cells[rowIterator, 1].Value.ToString()).Id; dbitem.NamaRuteTol = workSheet.Cells[rowIterator, 2].Value.ToString(); //child 1 int idx = 0; for (idx = rowIterator; idx <= noOfRow; idx++) { if (workSheet.Cells[idx, 1].Value == null || idx == rowIterator) { if (workSheet.Cells[idx, 3].Value != null) { Context.TolBerangkat item = new Context.TolBerangkat(); item.IdTol = RepoJnsTol.FindByNamaTol(workSheet.Cells[idx, 3].Value.ToString()).Id; dbitem.ListTolBerangkat.Add(item); } } else { break; } } //child 1 idx = 0; for (idx = rowIterator; idx <= noOfRow; idx++) { if (workSheet.Cells[idx, 1].Value == null || idx == rowIterator) { if (workSheet.Cells[idx, 4].Value != null) { Context.TolPulang item = new Context.TolPulang(); item.IdTol = RepoJnsTol.FindByNamaTol(workSheet.Cells[idx, 4].Value.ToString()).Id; dbitem.ListTolPulang.Add(item); } } else { break; } } if (idx != 0) { rowIterator = idx - 1; } RepoRuteTol.save(dbitem, UserPrincipal.id); } catch (Exception) { } } } } response.Success = true; } catch (Exception e) { response.Success = false; response.Message = e.Message.ToString(); } } } return(new JavaScriptSerializer().Serialize(new { Response = response })); }