public static void AgregarElementoElectrico(ElementoElectrico ee) { SqlConnection cn = new SqlConnection(cadConexion); try { SqlCommand cmd = new SqlCommand(); string consulta = "INSERT INTO ElementosElectricidad (nombre, idTipoElemElectricidad) VALUES (@nombre, @tipo)"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@nombre", ee.Nombre); cmd.Parameters.AddWithValue("@tipo", ee.Tipo); cmd.CommandType = CommandType.Text; cmd.CommandText = consulta; cn.Open(); cmd.Connection = cn; cmd.ExecuteNonQuery(); } catch (Exception e) { throw e; } finally { cn.Close(); } }
public static void EliminarElementoElectrico(ElementoElectrico ee) { SqlConnection cn = new SqlConnection(cadConexion); try { SqlCommand cmd = new SqlCommand(); string consulta = "DELETE FROM ElementosElectricidad WHERE nombre=@nom and idTipoElemElectricidad=@tipo and id=@id"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@id", ee.Id); cmd.Parameters.AddWithValue("@nom", ee.Nombre); cmd.Parameters.AddWithValue("@tipo", ee.Tipo); cmd.CommandType = CommandType.Text; cmd.CommandText = consulta; cn.Open(); cmd.Connection = cn; cmd.ExecuteNonQuery(); } catch (Exception) { throw; } finally { cn.Close(); } }
private void btnGuardarElemento_Click(object sender, EventArgs e) { try { ElementoElectrico ee = ObtenerDatosElemento(); AD_Electricidad.AgregarElementoElectrico(ee); CargarTablaElementos(); } catch (Exception ex) { MessageBox.Show("Error al guardar elemento"); } }
private void btnActualizar_Click(object sender, EventArgs e) { try { ElementoElectrico ee = ObtenerDatosElemento(); ee.Id = Int32.Parse(lblId.Text); AD_Electricidad.ActualizarElementoElectrico(ee); CargarTablaElementos(); } catch (Exception ex) { MessageBox.Show("Error al actualizar datos"); } }
private void btnEliminar_Click(object sender, EventArgs e) { try { ElementoElectrico ee = ObtenerDatosElemento(); ee.Id = Int32.Parse(lblId.Text); AD_Electricidad.EliminarElementoElectrico(ee); CargarTablaElementos(); } catch (Exception) { MessageBox.Show("Error al eliminar registro"); } }
private ElementoElectrico ObtenerDatosElemento() { try { ElementoElectrico ee = new ElementoElectrico(); ee.Nombre = txtNombre.Text; ee.Tipo = (int)cmbTipo.SelectedValue; return(ee); } catch (Exception) { MessageBox.Show("Error al obtener datos"); throw; } }
private void clickCelda(object sender, DataGridViewCellEventArgs e) { try { int index = e.RowIndex; if (index < 0) { } else { DataGridViewRow selectedRow = dataElectricidad.Rows[index]; string id = selectedRow.Cells[0].Value.ToString(); lblId.Text = id; ElementoElectrico ee = AD_Electricidad.buscarElemElectrico(id); txtNombre.Text = ee.Nombre; cmbTipo.SelectedValue = ee.Tipo; } } catch (Exception) { MessageBox.Show("Error al seleccionar elemento"); } }
internal static ElementoElectrico buscarElemElectrico(string id) { SqlConnection cn = new SqlConnection(cadConexion); try { SqlCommand cmd = new SqlCommand(); string consulta = "SELECT id, nombre, idTipoElemElectricidad FROM ElementosElectricidad where id=@id"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@id", id); cmd.CommandType = CommandType.Text; cmd.CommandText = consulta; cn.Open(); cmd.Connection = cn; cmd.ExecuteNonQuery(); SqlDataReader dr = cmd.ExecuteReader(); if (dr != null && dr.Read()) { int idBD = Int32.Parse(dr["id"].ToString()); string nombre = dr["nombre"].ToString(); int tipo = int.Parse(dr["idTipoElemElectricidad"].ToString()); return(new ElementoElectrico(idBD, nombre, tipo)); } else { ElementoElectrico ee = null; return(ee); } } catch (Exception) { throw; } }