public void RegistrarEntrada(EntradaConta obj) { try { openCon(); string str = string.Format(@"INSERT INTO dbo.Entrada VALUES(@id,@date,@nome,@tipo,@valor,@comentario)"); using (cmd = new SqlCommand(str, con)) { string data = DateTime.Now.ToString("yyyy/MM/dd"); cmd.Parameters.AddWithValue("@id", obj.id); cmd.Parameters.AddWithValue("@date", data); if (string.IsNullOrEmpty(obj.Nome)) { cmd.Parameters.AddWithValue("@nome", DBNull.Value); } else { cmd.Parameters.AddWithValue("@nome", obj.Nome.ToUpper()); } if (string.IsNullOrEmpty(obj.TipoEntrada)) { cmd.Parameters.AddWithValue("@tipo", DBNull.Value); } else { cmd.Parameters.AddWithValue("@tipo", obj.TipoEntrada.ToUpper()); } if (string.IsNullOrEmpty(obj.Valor)) { cmd.Parameters.AddWithValue("@valor", DBNull.Value); } else { cmd.Parameters.AddWithValue("@valor", obj.Valor); } if (string.IsNullOrEmpty(obj.Comentario)) { cmd.Parameters.AddWithValue("@comentario", DBNull.Value); } else { cmd.Parameters.AddWithValue("@comentario", obj.Comentario.ToUpper()); } cmd.ExecuteNonQuery(); } } catch (SqlException ex) { throw new Exception(ex.Message); } finally { CloseCon(); } }
public ActionResult CadastroEntrada([Bind(Include = "Id,Date,Nome,TipoEntrada,Valor,Comentario")] EntradaConta model) { try { Entrada db = new Entrada(); //ViewBag.Usuario = User.Identity.GetUserId(); model.id = User.Identity.GetUserId(); if (ModelState.IsValid) { db.RegistrarEntrada(model); ModelState.Clear(); ViewBag.MessageSuccess = "Registro feito com sucesso"; } } catch (Exception ex) { ViewBag.MessageError = "Erro no registro " + ex.Message; } return(View()); }