Sunteți pe pagina 1din 5

Mtodos a reutilizar

namespace Datos_Personal { public partial class FRM_Agregar : Form { public FRM_Agregar() { InitializeComponent();

} private void BTO_Agregar_Click(object sender, EventArgs e) { // LimpiarFormulario(); // Permite limpiar el formulario //Interface_inicial(); // prepara la interface inicial para agregar //try //{ AgregarRegistro(TEX_Cedula.Text, TEX_Nombre.Text, TEX_Apellidos.Text, TEX_Direccion.Text, TEX_Edad.Text, TEX_Telefono.Text); LimpiarFormulario(); Interface_inicial(); TEX_Nombre.Focus(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} } private bool AgregarRegistro(string cedula, String nombre, String apellidos, String direccion, string edad, string telefono) { //

int ced = Convert.ToInt32(cedula); int eda = Convert.ToInt32(edad); int tel = Convert.ToInt32(telefono); SqlConnection conne = new SqlConnection("Data Source=OFITEARADI1\\SQLEXPRESS;Initial Catalog=Telepol;Persist Security Info=True;User ID=sa;Password=Miyeya2011Miyeya"); // se abre la conexion conne.Open(); // cadena sql - comando para agregar a la base de datos string CadenaSQL = "INSERT INTO personal (Cedula, Nombre, Apellidos, Direccion, Edad, Telefono) VALUES (" + ced + ", '" + nombre + "', '" + apellidos + "', '" + direccion + "', " + eda + ", " + tel + ") ";

SqlCommand comando = conne.CreateCommand(); comando.CommandText = CadenaSQL; // se ejecuta la accion comando.ExecuteNonQuery(); // se cierra la conexion conne.Close();

return true; } private void LimpiarFormulario() { TEX_Apellidos.Clear(); TEX_Direccion.Clear(); TEX_Nombre.Clear(); TEX_Telefono.Clear(); TEX_Edad.Clear(); } private void BTO_Salir_Click(object sender, EventArgs e) { this.Close(); } private void FRM_Agregar_Load(object sender, EventArgs e) { Interface_inicial(); } private void Interface_inicial() { LBL_Apellidos.Enabled = false; LBL_Cedula.Enabled = true; LBL_Direccion.Enabled = false; LBL_Edad.Enabled = false; LBL_Telefono.Enabled = false; LBL_Nombre.Enabled = false; TEX_Apellidos.Enabled = false; TEX_Cedula.Enabled = true; TEX_Direccion.Enabled = false; TEX_Edad.Enabled = false; TEX_Nombre.Enabled = false; TEX_Telefono.Enabled = false; BTO_Agregar.Enabled = false; BTO_Buscar.Enabled = true; BTO_Salir.Enabled = true; TEX_Cedula.Clear(); TEX_Cedula.Focus(); } private void BTO_Buscar_Click(object sender, EventArgs e) {

if (Buscar_Registro(TEX_Cedula.Text) == false) { LimpiarFormulario(); Interface_Datos(); TEX_Cedula.Focus(); } else { MessageBox.Show("El registro existe..Intentelo de nuevo..gracias"); TEX_Cedula.Clear(); TEX_Cedula.Focus(); }

} private bool Buscar_Registro(String cedula) { int cedu = Convert.ToInt32(cedula);

SqlConnection conne = new SqlConnection("Data Source=OFITEARADI1\\SQLEXPRESS;Initial Catalog=Telepol;User ID=sa;Password=Miyeya2011Miyeya"); // se abre la conexion

// cadena sql - comando para agregar a la base de datos string CadenaSQL = "SELECT * FROM personal WHERE Cedula= " + cedu;

SqlDataAdapter da = new SqlDataAdapter(CadenaSQL, conne); DataSet ds = new DataSet(); conne.Open(); da.Fill(ds); conne.Close(); if (ds.Tables[0].Rows.Count == 0) { return false; } else { TEX_Nombre.Text = ds.Tables[0].Rows[0]["Nombre"].ToString(); TEX_Apellidos.Text = ds.Tables[0].Rows[0]["Apellidos"].ToString(); TEX_Direccion.Text = ds.Tables[0].Rows[0]["Direccion"].ToString(); TEX_Edad.Text = ds.Tables[0].Rows[0]["Edad"].ToString(); TEX_Telefono.Text = ds.Tables[0].Rows[0]["Telefono"].ToString(); return true;

} }

private void Interface_Datos() { LBL_Apellidos.Enabled = true; LBL_Cedula.Enabled = false; LBL_Direccion.Enabled = true; LBL_Edad.Enabled = true; LBL_Telefono.Enabled = true; LBL_Nombre.Enabled = true; TEX_Apellidos.Enabled = true; TEX_Cedula.Enabled = false; TEX_Direccion.Enabled = true; TEX_Edad.Enabled = true; TEX_Nombre.Enabled = true; TEX_Telefono.Enabled = true; BTO_Agregar.Enabled = true; BTO_Buscar.Enabled = false; BTO_Salir.Enabled = true; TEX_Nombre.Focus(); } private void TEX_Nombre_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { e.Handled = true; SendKeys.Send("{TAB}"); } e.Handled = Validadores.Validadores_Caracter(e.KeyChar); } private void TEX_Apellidos_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { e.Handled = true; SendKeys.Send("{TAB}"); } e.Handled = Validadores.Validadores_Caracter(e.KeyChar); } private void TEX_Direccion_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { e.Handled = true; SendKeys.Send("{TAB}"); } e.Handled = Validadores.Validadores_Caracter(e.KeyChar); } private void TEX_Edad_KeyPress(object sender, KeyPressEventArgs e) {

if (e.KeyChar == (char)13) { e.Handled = true; SendKeys.Send("{TAB}");

} e.Handled = Validadores.Validadores_Numericos(e.KeyChar); } private void TEX_Telefono_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { e.Handled = true; SendKeys.Send("{TAB}"); } e.Handled = Validadores.Validadores_Numericos(e.KeyChar); } private void TEX_Cedula_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { e.Handled = true; SendKeys.Send("{TAB}"); } e.Handled = Validadores.Validadores_Numericos(e.KeyChar); }

S-ar putea să vă placă și