// insert public int InsereEditor(Editores aoNovoEditor) { var liQtdRegistrosInseridos = 0; using (ioConexao = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { try { ioConexao.Open(); if (aoNovoEditor == null) { throw new NullReferenceException(); } ioQuery = new SqlCommand($@"INSERT INTO EDI_EDITORES({_id}, {_nome}, {_email}, {_url}) VALUES (@idEditor, @nomeEditor, @emailEditor, @urlEditor)", ioConexao); ioQuery.Parameters.Add(new SqlParameter("@idEditor", aoNovoEditor.edi_id_editor)); ioQuery.Parameters.Add(new SqlParameter("@nomeEditor", aoNovoEditor.edi_nm_editor)); ioQuery.Parameters.Add(new SqlParameter("@emailEditor", aoNovoEditor.edi_ds_email)); ioQuery.Parameters.Add(new SqlParameter("@urlEditor", aoNovoEditor.edi_ds_url)); liQtdRegistrosInseridos = ioQuery.ExecuteNonQuery(); } catch (Exception) { throw new Exception("Não foi possível enserir editor."); } } return(liQtdRegistrosInseridos); }
// update public int AtualizaEditor(Editores aoEditor) { var liQtdRegistrosAtualizados = 0; using (ioConexao = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { try { ioConexao.Open(); if (aoEditor == null) { throw new NullReferenceException(); } ioQuery = new SqlCommand( $@"UPDATE EDI_EDITORES SET {_nome} = @nomeEditor, {_email} = @emailEditor, {_url} = @urlEditor WHERE {_id} = @idEditor", ioConexao); ioQuery.Parameters.Add(new SqlParameter("@idEditor", aoEditor.edi_id_editor)); ioQuery.Parameters.Add(new SqlParameter("@nomeEditor", aoEditor.edi_nm_editor)); ioQuery.Parameters.Add(new SqlParameter("@emailEditor", aoEditor.edi_ds_email)); ioQuery.Parameters.Add(new SqlParameter("@urlEditor", aoEditor.edi_ds_url)); liQtdRegistrosAtualizados = ioQuery.ExecuteNonQuery(); } catch (Exception) { throw; } } return(liQtdRegistrosAtualizados); }
// delete public int DeletaEditor(Editores aoEditor) { var liQtdRegistrosDeletados = 0; using (ioConexao = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { try { ioConexao.Open(); if (aoEditor == null) { throw new NullReferenceException(); } ioQuery = new SqlCommand( $@"DELETE FROM EDI_EDITORES WHERE {_id} = @idEditor", ioConexao); ioQuery.Parameters.Add(new SqlParameter("@idEditor", aoEditor.edi_id_editor)); liQtdRegistrosDeletados = ioQuery.ExecuteNonQuery(); } catch (Exception) { throw; } } return(liQtdRegistrosDeletados); }
protected void btnNovoEditor_Click(object sender, EventArgs e) { try { // pegar os valores de Editor // ultimo id criado + 1 decimal ldcIdEditor = ListaEditores.OrderByDescending(editor => editor.edi_id_editor).FirstOrDefault().edi_id_editor + 1; // nome string lsNomeEditor = tbxCadastroNomeEditor.Text; // email string lsEmailEditor = tbxCadastroEmailEditor.Text; // url string lsUrlEditor = tbxCadastroUrlEditor.Text; // criar o Editor Editores loEditor = new Editores(ldcIdEditor, lsNomeEditor, lsEmailEditor, lsUrlEditor); ioEditoresDAO.InsereEditor(loEditor); CarregaDados(); // sucesso HttpContext.Current.Response.Write("<script>alert('Editor cadastrado com sucesso!');</script>"); } catch (Exception ex) { // erro HttpContext.Current.Response.Write("<script>alert('Editor não pôde ser cadastrado!');</script>"); HttpContext.Current.Response.Write($"<script>console.log({ex.StackTrace});</script>"); HttpContext.Current.Response.Write($"<script>console.log({ex.Message});</script>"); } // apaga os campos preenchidos tbxCadastroNomeEditor.Text = String.Empty; tbxCadastroEmailEditor.Text = String.Empty; tbxCadastroUrlEditor.Text = String.Empty; }
protected void gvGerenciamentoEditores_RowUpdating(object sender, GridViewUpdateEventArgs e) { var currentRow = gvGerenciamentoEditores.Rows[e.RowIndex]; // pegar os valores sob a mascaraca de edição // id decimal ldcIdEditor = Convert.ToDecimal((currentRow.FindControl("lblEditIdEditor") as Label).Text); // nome string lsNomeEditor = (currentRow.FindControl("tbxEditNomeEditor") as TextBox).Text; // email string lsEmailEditor = (currentRow.FindControl("tbxEditEmailEditor") as TextBox).Text; // url string lsUrlEditor = (currentRow.FindControl("tbxEditUrlEditor") as TextBox).Text; // verificar se são diferentes de espaço em branco if (String.IsNullOrWhiteSpace(lsNomeEditor)) { HttpContext.Current.Response.Write("<script>alert('Preencha o campo de nome!');</script>"); } else if (String.IsNullOrWhiteSpace(lsEmailEditor)) { HttpContext.Current.Response.Write("<script>alert('Preencha o campo de email!');</script>"); } else if (String.IsNullOrWhiteSpace(lsUrlEditor)) { HttpContext.Current.Response.Write("<script>alert('Preencha o campo de url!');</script>"); } else { try { Editores loEditor = new Editores(ldcIdEditor, lsNomeEditor, lsEmailEditor, lsUrlEditor); // usar o DAO para atualizar o Editor ioEditoresDAO.AtualizaEditor(loEditor); // resetar o index gvGerenciamentoEditores.EditIndex = -1; // recarregar a lista de editores CarregaDados(); // sucesso HttpContext.Current.Response.Write("<script>alert('Editor atualizado com sucesso!');</script>"); } catch (Exception ex) { // caso erro HttpContext.Current.Response.Write("<script>alert('Ocorreu um problema ao atualizar Editor.');</script>"); HttpContext.Current.Response.Write($"<script>console.log({ex.StackTrace});</script>"); HttpContext.Current.Response.Write($"<script>console.log({ex.Message});</script>"); } } }
public BindingList <Livros> FindLivrosByEditor(Editores ioEditor) { var ioListLivros = new BindingList <Livros>(); using (ioConexao = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { ioConexao.Open(); try { if (ioEditor == null) { throw new ArgumentException("Autor para busca não foi informado"); } else { ioQuery = new SqlCommand( @"SELECT * FROM LIV_LIVROS INNER JOIN EDI_EDITORES on EDI_ID_EDITOR = LIV_ID_EDITOR WHERE EDI_ID_EDITOR = @idEditor", ioConexao); ioQuery.Parameters.Add(new SqlParameter("@idEditor", ioEditor.edi_id_editor)); } using (SqlDataReader loReader = ioQuery.ExecuteReader()) { while (loReader.Read()) { ioListLivros.Add(new Livros( loReader.GetDecimal(0), loReader.GetDecimal(1), loReader.GetDecimal(2), loReader.GetString(3), loReader.GetDecimal(4), loReader.GetDecimal(5), loReader.GetString(6), loReader.GetInt32(7) )); } } } catch (Exception) { throw; } } return(ioListLivros); }
protected void gvGerenciamentoEditores_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { decimal ldcIdeditor = Convert.ToDecimal((gvGerenciamentoEditores.Rows[e.RowIndex].FindControl("lblIdEditor") as Label).Text); // buscar o editor Editores loEditor = ioEditoresDAO.BuscaEditores(ldcIdeditor).FirstOrDefault(); if (loEditor != null) { //Crie LivrosDAO e o método FindLivrosByEditor() que deve receber um Editor como parâmetro e retornar //uma lista de livros. LivrosDAO loLivrosDAO = new LivrosDAO(); if (loLivrosDAO.FindLivrosByEditor(loEditor).Count > 0) { // se a busca retornar pelo menos 1 livro, não pode deletar o editor HttpContext.Current.Response.Write("<script>alert('Não é possível remover o editor selecionado pois existem livros associados a ele.');</script>"); } else { // deletar o editor ioEditoresDAO.DeletaEditor(loEditor); // sucesso HttpContext.Current.Response.Write("<script>alert('Editor removido com sucesso!');</script>"); // reseta o index gvGerenciamentoEditores.EditIndex = -1; // recarrega os dados CarregaDados(); } } } catch (Exception ex) { // erro HttpContext.Current.Response.Write("<script>alert('Editor removido com sucesso!');</script>"); HttpContext.Current.Response.Write($"<script>console.log({ex.Message});</script>"); HttpContext.Current.Response.Write($"<script>console.log({ex.StackTrace});</script>"); } }