Advertisement
plattina

Untitled

Jan 28th, 2023
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | Source Code | 0 0
  1. using AtivaCliente.Models;
  2.  
  3. namespace AtivaCliente
  4. {
  5.     public partial class Form1 : Form
  6.     {
  7.         private readonly meu_bancoContext _context;
  8.  
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.             _context= new meu_bancoContext();
  13.         }
  14.  
  15.         private void btnPesquisarCodigo_Click(object sender, EventArgs e)
  16.         {
  17.             if (!string.IsNullOrEmpty(txtCodigoUsuario.Text))
  18.             {
  19.                 Usuario usuario = _context.Usuarios.FirstOrDefault(x => x.Id == int.Parse(txtCodigoUsuario.Text));
  20.  
  21.                 if (usuario == null)
  22.                 {
  23.                     MessageBox.Show("Usuário não encontrado");
  24.                     return;
  25.                 }
  26.  
  27.                 txtNome.Text = usuario.Nome;
  28.                 txtEmail.Text = usuario.Email;
  29.                 chkAtivo.Checked = usuario.Ativo;
  30.             }
  31.             else
  32.             {
  33.                 MessageBox.Show("Informar um código de usuário!");
  34.             }
  35.         }
  36.  
  37.         private void btnCancelar_Click(object sender, EventArgs e)
  38.         {
  39.  
  40.             if (!string.IsNullOrEmpty(txtCodigoUsuario.Text))
  41.             {
  42.                 Usuario usuario = _context.Usuarios.FirstOrDefault(x => x.Id == int.Parse(txtCodigoUsuario.Text));
  43.  
  44.                 if (usuario == null)
  45.                 {
  46.                     MessageBox.Show("Usuário não encontrado");
  47.                     return;
  48.                 }
  49.  
  50.                 usuario.Ativo = chkAtivo.Checked;
  51.                 _context.SaveChanges();
  52.                 MessageBox.Show("Usuário Salvo Com Sucesso!");
  53.                 return;              
  54.             }
  55.             else
  56.             {
  57.                 MessageBox.Show("Informar um código de usuário!");
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement