Пример #1
0
 public Kontak(Context.Kontak dbitem)
 {
     Id          = dbitem.Id;
     Nama        = dbitem.Nama;
     IdJabatan   = dbitem.IdJabatan;
     NamaJabatan = dbitem.LookUpCodeJabatan.Nama;
     Hp          = dbitem.Hp;
     Email       = dbitem.Email;
 }
Пример #2
0
        public string Upload(IEnumerable <HttpPostedFileBase> files)
        {
            ResponeModel response = new ResponeModel();

            //algoritma
            if (files != null)
            {
                foreach (var file in files)
                {
                    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 && workSheet.Cells[rowIterator, 3].Value != null &&
                                    workSheet.Cells[rowIterator, 4].Value != null && workSheet.Cells[rowIterator, 5].Value != null)
                                {
                                    int id = 0;

                                    int resId;
                                    if (workSheet.Cells[rowIterator, 6].Value != null)
                                    {
                                        if (int.TryParse(workSheet.Cells[rowIterator, 6].Value.ToString(), out resId))
                                        {
                                            id = resId;
                                        }
                                    }
                                    //cara gancang ngarah teu kudu aya pengecekan tiap field
                                    Context.VendorGps dbitem = new Context.VendorGps();
                                    try
                                    {
                                        if (id != 0)
                                        {
                                            dbitem = RepoVendor.FindByPK(id);
                                        }

                                        dbitem.Nama   = workSheet.Cells[rowIterator, 1].Value.ToString();
                                        dbitem.Alamat = workSheet.Cells[rowIterator, 2].Value.ToString();
                                        dbitem.Telp   = workSheet.Cells[rowIterator, 3].Value.ToString();
                                        dbitem.Email  = workSheet.Cells[rowIterator, 4].Value.ToString();
                                        dbitem.Web    = workSheet.Cells[rowIterator, 5].Value.ToString();

                                        RepoVendor.save(dbitem, UserPrincipal.id);
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }

                            //sheet 2
                            workSheet = currentSheet.Where(s => s.Index == 2).FirstOrDefault();
                            noOfRow   = workSheet.Dimension.End.Row;

                            for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                            {
                                if (workSheet.Cells[rowIterator, 1].Value != null && workSheet.Cells[rowIterator, 2].Value != null && workSheet.Cells[rowIterator, 3].Value != null &&
                                    workSheet.Cells[rowIterator, 4].Value != null && workSheet.Cells[rowIterator, 5].Value != null)
                                {
                                    if (workSheet.Cells[rowIterator, 6].Value.ToString() != null && workSheet.Cells[rowIterator, 7].Value.ToString() != null)
                                    {
                                        //edit
                                        try
                                        {
                                            Context.VendorGps db  = RepoVendor.FindByPK(int.Parse(workSheet.Cells[rowIterator, 6].Value.ToString()));
                                            int            iditem = int.Parse(workSheet.Cells[rowIterator, 7].Value.ToString());
                                            Context.Kontak dbitem = db.ListKontak.Where(d => d.Id == iditem).FirstOrDefault();
                                            dbitem.Nama      = workSheet.Cells[rowIterator, 2].Value.ToString();
                                            dbitem.IdJabatan = RepoLookup.FindByName(workSheet.Cells[rowIterator, 3].Value.ToString()).Id;
                                            dbitem.Hp        = workSheet.Cells[rowIterator, 4].Value.ToString();
                                            dbitem.Email     = workSheet.Cells[rowIterator, 5].Value.ToString();
                                            RepoVendor.save(db, UserPrincipal.id);
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                    else
                                    {
                                        //add
                                        try
                                        {
                                            Context.VendorGps db     = RepoVendor.FindByPK(int.Parse(workSheet.Cells[rowIterator, 6].Value.ToString()));
                                            Context.Kontak    dbitem = new Context.Kontak();
                                            dbitem.Nama      = workSheet.Cells[rowIterator, 2].Value.ToString();
                                            dbitem.IdJabatan = RepoLookup.FindByName(workSheet.Cells[rowIterator, 3].Value.ToString()).Id;
                                            dbitem.Hp        = workSheet.Cells[rowIterator, 4].Value.ToString();
                                            dbitem.Email     = workSheet.Cells[rowIterator, 5].Value.ToString();
                                            db.ListKontak.Add(dbitem);
                                            RepoVendor.save(db, 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 }));
        }