コード例 #1
0
 private void Eliminar(Categorias categorias)
 {
     using (var db = new Bd())
     {
         db.Entry <Categorias>(categorias).State = EntityState.Deleted;
         db.SaveChanges();
     }
     mostrarDatos();
 }
コード例 #2
0
        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                {
                    var categoria = new Categorias();
                    categoria.Descripcion = TxtDescripcion.Text;
                    categoria.Activa      = ChActivo.Checked;

                    if (!(TxtId.Text.Trim() == string.Empty))
                    {
                        categoria.Id = Convert.ToInt32(TxtId.Text);
                    }

                    using (var db = new Bd())
                    {
                        if (categoria.Id == 0)
                        {
                            db.Categorias.Add(categoria);
                        }

                        else
                        {
                            db.Entry
                                (categoria).State = EntityState.Modified;
                        }
                        {
                            if (db.SaveChanges() != 0)
                            {
                                MessageBox.Show("Registro guardado Exitosamente", "Guardado", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            BtnGuardar.Enabled  = false;
                            BtnEliminar.Enabled = false;
                            Limpiar();
                            mostrarDatos();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }