private void btnEliminar_Click(object sender, EventArgs e) { try { string message = "¿Estas seguro de eliminar el registo?"; string title = "Eliminar"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result = MessageBox.Show(message, title, buttons); if (result == DialogResult.Yes) { int?Id_Tipos_Combustible = GetId(); if (Id_Tipos_Combustible != null) { using (rentcarEntities db = new rentcarEntities()) { Models.Tipos_Combustibles oTipos_Combustible = db.Tipos_Combustibles.Find(Id_Tipos_Combustible); db.Tipos_Combustibles.Remove(oTipos_Combustible); db.SaveChanges(); } Refresh(); } } } catch { MessageBox.Show("Este registro esta enzalado a otra tabla."); } }
private void LoadData() { using (rentcarEntities db = new rentcarEntities()) { oTipos_Combustibles = db.Tipos_Combustibles.Find(Id_Tipos_Combustible); txtDescripcion.Text = oTipos_Combustibles.Descripcion; cmbEstado.Text = oTipos_Combustibles.Estado; } }
private void btnGuardar_Click(object sender, EventArgs e) { try { using (rentcarEntities db = new rentcarEntities()) { if (Id_Tipos_Combustible == null) { oTipos_Combustibles = new Models.Tipos_Combustibles(); } if (txtDescripcion.Text.Trim().Equals("") || cmbEstado.Text.Trim().Equals("")) { MessageBox.Show("Por favor, llenar todos los campos."); } else { var exists = db.Tipos_Combustibles.Any(x => x.Descripcion.Equals(txtDescripcion.Text)); if (exists && Id_Tipos_Combustible == null) { MessageBox.Show("Este tipo de combustible ya habia sido registrado."); return; } else { oTipos_Combustibles.Descripcion = txtDescripcion.Text; oTipos_Combustibles.Estado = cmbEstado.Text; if (Id_Tipos_Combustible == null) { db.Tipos_Combustibles.Add(oTipos_Combustibles); } else { db.Entry(oTipos_Combustibles).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); MessageBox.Show("Guardado exitosamente"); this.Close(); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }