Ejemplo n.º 1
0
        public ActionResult DeleteConfirmed(short id)
        {
            GUsuario gusuario = db.GUsuario.Find(id);

            db.GUsuario.Remove(gusuario);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public ActionResult Delete(short id = 0)
        {
            GUsuario gusuario = db.GUsuario.Find(id);

            if (gusuario == null)
            {
                return(HttpNotFound());
            }
            return(View(gusuario));
        }
Ejemplo n.º 3
0
 public ActionResult Edit(GUsuario gusuario)
 {
     if (ModelState.IsValid)
     {
         db.Entry(gusuario).State = EntityState.Modified;
         if (!gusuario.CD_DEPARTAMENTO.HasValue)
         {
             gusuario.CD_DEPARTAMENTO = 0;
         }
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(gusuario));
 }
Ejemplo n.º 4
0
        public ActionResult Create(GUsuario gusuario)
        {
            if (ModelState.IsValid)
            {
                Int32?intcd_gusuario = db.GUsuario.Max(s => (Int32?)s.CD_GUSUARIO);

                if (intcd_gusuario != null)
                {
                    intcd_gusuario++;
                }
                else
                {
                    intcd_gusuario = 1;
                }

                gusuario.CD_GUSUARIO = (Int32)intcd_gusuario;



                //  db.GUsuario.Add(gusuario);
                try
                {
                    if (!gusuario.CD_DEPARTAMENTO.HasValue)
                    {
                        gusuario.CD_DEPARTAMENTO = 0;
                    }
                    string sql;
                    sql = string.Format(" INSERT INTO GUSUARIO VALUES ({0},\'{1}\', \'{2}\', {3})", gusuario.CD_GUSUARIO, gusuario.NOME, gusuario.CD_DEPARTAMENTO, gusuario.CD_PAGINA);
                    db.Database.ExecuteSqlCommand(sql);
                    //db.SaveChanges();
                }
                catch (Exception error)
                {
                    throw new Exception(error.ToString());
                }

                return(RedirectToAction("Index"));
            }

            return(View(gusuario));
        }
Ejemplo n.º 5
0
        public ActionResult Save(IEnumerable <HttpPostedFileBase> attachments)
        {
            // The Name of the Upload component is "attachments"
            foreach (var file in attachments)
            {
                // Some browsers send file names with full path. This needs to be stripped.
                var    fileName     = Path.GetFileName(file.FileName);
                var    physicalPath = Path.Combine(Server.MapPath("~/App_Imports"), fileName);
                string exteension   = Path.GetExtension(fileName);
                // The files are not actually saved in this demo
                int counter  = 1;
                int verifica = 0;
                while (System.IO.File.Exists(physicalPath))
                {
                    counter++;
                    physicalPath = Path.Combine(HttpContext.Server.MapPath("~/App_Imports/"),
                                                Path.GetFileNameWithoutExtension(fileName) + counter.ToString() + Path.GetExtension(fileName));
                }
                file.SaveAs(physicalPath);


                DataSet dss = new DataSet();
                string  ConnectionString = "";
                if (exteension == ".xls")
                {
                    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + physicalPath + ";Extended Properties=Excel 8.0;";
                    verifica         = 1;
                }

                if (exteension == ".xlsx")
                {
                    ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + physicalPath + ";Extended Properties=Excel 12.0;";
                    verifica         = 1;
                }

                if (verifica == 0)
                {
                    throw new Exception("Extensão não suportadaErro ao Salvar");
                }



                using (OleDbConnection conn = new System.Data.OleDb.OleDbConnection(ConnectionString))
                {
                    conn.Open();
                    using (DataTable dtExcelSchema = conn.GetSchema("Tables"))
                    {
                        string           sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                        string           query     = "SELECT * FROM [" + sheetName + "]";
                        OleDbDataAdapter adapter   = new OleDbDataAdapter(query, conn);

                        adapter.Fill(dss, "Items");

                        if (dss.Tables.Count > 0)
                        {
                            if (dss.Tables[0].Rows.Count > 0)
                            {
                                try
                                {
                                    for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
                                    {
                                        GUsuario gusuario = new GUsuario();
                                        int      id;

                                        // colocar as colunas aqui para importacao
                                        //tentar customizar no .tt
                                        // na index desta controller ao final do arquivo, gerou um codigo padrao para colocar aqui



                                        try
                                        {
                                            db.GUsuario.Add(gusuario);
                                        }
                                        catch (Exception erro)
                                        {
                                            throw new Exception(erro.ToString());
                                            //return RedirectToAction("ErroAoSalvar");
                                        }


                                        gusuario = null;
                                    }
                                }
                                catch (Exception erro)
                                {
                                    string err = "<b>Erro Gerado na importação do arquivo, consulte os detalhes para mais informações </b> ";
                                    err += "</br>";
                                    err += _Funcoes.TrataErro(erro);
                                    err += "</br>";
                                    throw new Exception(err.ToString());
                                }

                                try
                                {
                                    db.SaveChanges();
                                    return(RedirectToAction("Index"));
                                }
                                catch (Exception dbEx)
                                {
                                    if (dbEx is System.Data.Entity.Validation.DbEntityValidationException)
                                    {
                                        string errors = "O Arquivo não é válido, verifique as propriedades abaixo para mais detalhes </br> "; // dbEx.EntityValidationErrors.First(); //.ValidationErrors.First();
                                        errors += "<b> Nenhum registro foi gravado.</b> A importação só será possível com o arquivo 100% correto. </br> ";

                                        DbEntityValidationException ex = (DbEntityValidationException)dbEx;
                                        foreach (var validationErrors in ex.EntityValidationErrors)
                                        {
                                            foreach (var validationError in validationErrors.ValidationErrors)
                                            {
                                                errors += string.Format(" A propriedade : <b>{0}</b> não foi validado devido ao erro: <b> {1} </b>", validationError.PropertyName, validationError.ErrorMessage) + "</br>";
                                            }
                                        }
                                        throw new Exception(errors.ToString());
                                    }
                                    else if (dbEx is System.Data.Entity.Infrastructure.DbUpdateException)
                                    {
                                        string err = "<b>Erro Gerado, consulte os detalhes para mais informações </b> ";
                                        err += "</br>";
                                        err += _Funcoes.TrataErro(dbEx);
                                        err += "</br>";
                                        err += dbEx.InnerException.InnerException.ToString();
                                        throw new Exception(err.ToString());
                                    }
                                    else
                                    {
                                        string err = "<b>Erro Gerado, consulte os detalhes para mais informações </b> ";
                                        err += "</br>";
                                        err += _Funcoes.TrataErro(dbEx);
                                        err += "</br>";
                                        throw new Exception(err.ToString());
                                    }
                                }
                            }
                        }
                    }
                }

                // Return an empty string to signify success
            }
            return(Content(""));
        }