protected void btn_Remover_Click(object sender, EventArgs e) { if (grid.SelectedValue != null) { //string login = grid.SelectedValue.ToString(); // Linha que deu origem ao comando abaixo. string ID = grid.SelectedValue.ToString(); try { using (ARCOS_Entities entities = new ARCOS_Entities()) { // USUARIO usuario = entities.USUARIO.FirstOrDefault(x => x.LOGIN.Equals(login)); // Linha que deu origem ao comando abaixo. VOLUNTARIO voluntario = entities.VOLUNTARIO.FirstOrDefault(x => x.ID.ToString().Equals(ID)); entities.VOLUNTARIO.Remove(voluntario); entities.SaveChanges(); //Limpar Grid grid.DataSource = null; grid.DataBind(); grid.SelectedIndex = -1; Response.Write("<script>alert('Voluntário removido com sucesso!');</script>"); } } catch { Response.Write("<script>alert('Falha ao remover registro!');</script>"); } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { String id = Request.QueryString["id"]; if ((id != null) && (!id.Equals(""))) { lblID.Text = id; using (ARCOS_Entities entities = new ARCOS_Entities()) { VOLUNTARIO u = entities.VOLUNTARIO.FirstOrDefault(x => x.ID.ToString().Equals(id)); if (u != null) { txt_vnome.Text = u.NOME; txt_vcpf.Text = u.CPF; txt_vendereco.Text = u.LOGRADOURO; txt_vnumero.Text = u.NUMERO; txt_vBairro.Text = u.BAIRRO; txt_vcep.Text = u.CEP; txt_vCidade.Text = u.CIDADE; txt_vDispo.Text = u.DISPONIBILIDADE; txt_vSerDisp.Text = u.SERVICOS_DISPONIVEIS; drp_vEstado.SelectedValue = u.ESTADO; ckb_vativo.Checked = u.ATIVO; lbl_Status.Text = "Alterando"; } } } } }
private void CarregaVoluntario() { VOLUNTARIO voluntario_blank = new VOLUNTARIO(); using (ARCOS_Entities entity = new ARCOS_Entities()) { List <VOLUNTARIO> lista = entity.VOLUNTARIO.ToList(); Ddl_voluntario.DataSource = lista; Ddl_voluntario.DataTextField = "NOME"; Ddl_voluntario.DataValueField = "ID"; Ddl_voluntario.DataBind(); } }
protected void btn_Cadastrar_Click(object sender, EventArgs e) { try { using (ARCOS_Entities entities = new ARCOS_Entities()) { VOLUNTARIO voluntario = new VOLUNTARIO(); if (txt_vnome.Text == "" || txt_vcpf.Text == "" || txt_vendereco.Text == "" || txt_vnumero.Text == "" || txt_vBairro.Text == "" || txt_vcep.Text == "" || txt_vCidade.Text == "" || drp_vEstado.Text == "" || txt_vDispo.Text == "" || txt_vSerDisp.Text == "") { Response.Write("<script>alert('Há campos obrigatórios não preenchidos!');</script>"); } else { if (lbl_Status.Text.Equals("Cadastrando")) { voluntario.NOME = txt_vnome.Text; voluntario.CPF = txt_vcpf.Text; voluntario.LOGRADOURO = txt_vendereco.Text; voluntario.NUMERO = txt_vnumero.Text; voluntario.BAIRRO = txt_vBairro.Text; voluntario.CEP = txt_vcep.Text; voluntario.CIDADE = txt_vCidade.Text; voluntario.ESTADO = drp_vEstado.Text; voluntario.DISPONIBILIDADE = txt_vDispo.Text; voluntario.SERVICOS_DISPONIVEIS = txt_vSerDisp.Text; voluntario.DATA_HORA_CRIACAO_REGISTRO = DateTime.Now; txt_vnome.Text = string.Empty; txt_vcpf.Text = string.Empty; txt_vendereco.Text = string.Empty; txt_vnumero.Text = string.Empty; txt_vBairro.Text = string.Empty; txt_vcep.Text = string.Empty; txt_vCidade.Text = string.Empty; drp_vEstado.SelectedValue = null; txt_vDispo.Text = string.Empty; txt_vSerDisp.Text = string.Empty; ckb_vativo.Checked = false; entities.VOLUNTARIO.Add(voluntario); } else { voluntario = entities.VOLUNTARIO.FirstOrDefault(x => x.ID.ToString().Equals(lblID.Text)); voluntario.NOME = txt_vnome.Text; voluntario.CPF = txt_vcpf.Text; voluntario.LOGRADOURO = txt_vendereco.Text; voluntario.NUMERO = txt_vnumero.Text; voluntario.BAIRRO = txt_vBairro.Text; voluntario.CEP = txt_vcep.Text; voluntario.CIDADE = txt_vCidade.Text; voluntario.ESTADO = drp_vEstado.Text; voluntario.DISPONIBILIDADE = txt_vDispo.Text; voluntario.SERVICOS_DISPONIVEIS = txt_vSerDisp.Text; voluntario.DATA_HORA_CRIACAO_REGISTRO = DateTime.Now; entities.Entry(voluntario); } } entities.SaveChanges(); Response.Write("<script>alert('Voluntario Cadastrado com Sucesso!');</script>"); } } catch { Response.Write("<script>alert('Registro não pode ser salvo!');</script>"); } }