Código de Clientes – Cambios [ Realizar cambios SQL desde C#]

Anuncio
Código de Clientes - Cambios
Esta ventana compone el registro de los clientes directamente a la Base de Datos
“Negocio”. Este Formulario es similar al registro de Altas, con la diferencia que consta de
un ComboBox que muestra los campos del registro para poder así modificar los datos
Funciones:
Llenar la lista del comboBox leyendo directamente desde la Base de Datos
Llenar todos los textBox leyendo información de la BD desde el botón mostrar
Restringir el tipo de escritura dentro de los textBox (tamaño y tipo de caracteres)
Verificar I D y que los campos necesarios no se encuentren vacíos.
Agregar los nuevos clientes mediante instrucciones SQL
Coding:
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Data.SqlClient;
namespace GoldenStar
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
System.Data.SqlClient.SqlConnection con;
private void pictureBox1_MouseMove(object sender, MouseEventArgs
e) { pictureBox1.Size = new Size(90, 90); }
private void pictureBox1_MouseLeave(object sender, EventArgs e) {
pictureBox1.Size = new Size(80, 80); }
private void pictureBox2_MouseMove(object sender, MouseEventArgs
e) { pictureBox2.Size = new Size(90, 90); }
private void pictureBox2_MouseLeave(object sender, EventArgs e) {
pictureBox2.Size = new Size(80, 80); }
private void pictureBox1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" ||
textBox5.Text == "" || textBox7.Text == "" || textBox8.Text == "" ||
textBox9.Text == "" || textBox10.Text == "" || textBox12.Text == "" ||
textBox13.Text == "")
{
MessageBox.Show("Faltan datos por llenar");
}
else
{
if (MessageBox.Show("Modificar registro de la Base de
Datos?", "Modificar cliente", MessageBoxButtons.YesNo) ==
DialogResult.Yes)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = Global.cadena;
try
{
string q = "update clientes set
nombre=@nombre,rfc=@rfc,tel1=@tel1,tel2=@tel2,calle=@calle,num_ext=@num_e
xt,num_int=@num_int,cp=@cp,colonia=@colonia,ciudad=@ciudad,estado=@estado
,pais=@pais where id_cte=@id_cte";
SqlCommand ORDEN = new SqlCommand(q, con);
ORDEN.Parameters.Add(new SqlParameter("@id_cte",
textBox11.Text));
ORDEN.Parameters.Add(new SqlParameter("@nombre",
textBox1.Text));
ORDEN.Parameters.Add(new SqlParameter("@rfc",
textBox2.Text));
ORDEN.Parameters.Add(new SqlParameter("@tel1",
textBox3.Text));
ORDEN.Parameters.Add(new SqlParameter("@tel2",
textBox4.Text));
ORDEN.Parameters.Add(new SqlParameter("@calle",
textBox5.Text));
ORDEN.Parameters.Add(new SqlParameter("@num_ext",
textBox7.Text));
ORDEN.Parameters.Add(new SqlParameter("@num_int",
textBox6.Text));
ORDEN.Parameters.Add(new SqlParameter("@cp",
textBox8.Text));
ORDEN.Parameters.Add(new SqlParameter("@colonia",
textBox9.Text));
ORDEN.Parameters.Add(new SqlParameter("@ciudad",
textBox10.Text));
ORDEN.Parameters.Add(new SqlParameter("@estado",
textBox12.Text));
ORDEN.Parameters.Add(new SqlParameter("@pais",
textBox13.Text));
ORDEN.Connection.Open();
ORDEN.ExecuteNonQuery();
ORDEN.Connection.Close();
MessageBox.Show("Registro modificado");
this.Close();
}
catch (Exception)
{
MessageBox.Show("Conexión fallida");
}
}
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("¿Seguro que quiere salir del registro?",
"Cancelar", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Close();
}
}
//Nombre - Solo letras con espacios
private void textBox1_KeyPress(object sender, KeyPressEventArgs
e)
{
if (Char.IsDigit(e.KeyChar)) { e.Handled = true; }
else if (Char.IsControl(e.KeyChar)) { e.Handled = false; }
else if (Char.IsSeparator(e.KeyChar)) { e.Handled = false; }
else { e.Handled = false; }
}
//Tel 1 - Solo numeros sin espacios
private void textBox3_KeyPress(object sender, KeyPressEventArgs
e)
{
if (Char.IsLetter(e.KeyChar)) { e.Handled = true; }
else if (Char.IsControl(e.KeyChar)) { e.Handled = false; }
else if (Char.IsSeparator(e.KeyChar)) { e.Handled = true; }
else { e.Handled = false; }
}
//Tel 2 - Solo numeros sin espacios
private void textBox4_KeyPress_1(object sender, KeyPressEventArgs
e)
{
if (Char.IsLetter(e.KeyChar)) { e.Handled = true; }
else if (Char.IsControl(e.KeyChar)) { e.Handled = false; }
else if (Char.IsSeparator(e.KeyChar)) { e.Handled = true; }
else { e.Handled = false; }
}
//RFC - Cualquier caracter sin espacios
private void textBox2_KeyPress(object sender, KeyPressEventArgs
e)
{
if (Char.IsControl(e.KeyChar)) { e.Handled = false; }
else if (Char.IsSeparator(e.KeyChar)) { e.Handled = true; }
else { e.Handled = false; }
}
//Num ext - Solo numeros sin espacios
private void textBox7_KeyPress_1(object sender, KeyPressEventArgs
e)
{
if (Char.IsLetter(e.KeyChar)) { e.Handled = true; }
else if (Char.IsControl(e.KeyChar)) { e.Handled = false; }
else if (Char.IsSeparator(e.KeyChar)) { e.Handled = true; }
else { e.Handled = false; }
}
//Num int - Solo numeros sin espacios
private void textBox6_KeyPress(object sender, KeyPressEventArgs
e)
{
if (Char.IsLetter(e.KeyChar)) { e.Handled = true; }
else if (Char.IsControl(e.KeyChar)) { e.Handled = false; }
else if (Char.IsSeparator(e.KeyChar)) { e.Handled = true; }
else { e.Handled = false; }
}
//CP - Solo numeros sin espacios
private void textBox8_KeyPress(object sender, KeyPressEventArgs
e)
{
if (Char.IsLetter(e.KeyChar)) { e.Handled = true; }
else if (Char.IsControl(e.KeyChar)) { e.Handled = false; }
else if (Char.IsSeparator(e.KeyChar)) { e.Handled = true; }
else { e.Handled = false; }
}
private void Form5_Load(object sender, EventArgs e)
{
llenarComboBox(); limpiar();
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = Global.cadena;
try
{
con.Open();
label12.Text = "Base de datos Conectada";
//Cuento registros
string strSql = "SELECT Count(id_cte) FROM clientes";
SqlCommand cmd = new SqlCommand(strSql, con);
int numRegistros = ((int)cmd.ExecuteScalar()) + 1;
con.Close();
label13.Text = "Registros contados: " + (numRegistros 1).ToString();
}
catch (Exception)
{
MessageBox.Show("Conexión fallida");
this.Close();
}
}
// **** Funciones de lectura de registro de la Base de Datos ****
//
private string leerClientes(string columna)
{
string cadena = "";
SqlCommand ORDEN = new SqlCommand("SELECT "+columna+" from
clientes where id_cte= '" + comboBox1.Text + "'", con);
ORDEN.Connection.Open();
ORDEN.ExecuteNonQuery();
SqlDataReader reader = ORDEN.ExecuteReader();
reader.Read();
try
{
if (reader.HasRows)
{ cadena = reader.GetString(0); }
}
catch (Exception)
{
cadena = "";
}
reader.Close(); ORDEN.Connection.Close();
return cadena;
}
private void button1_Click(object sender, EventArgs e)
{
if (leerClientes("id_cte") != "")
{
//ID
textBox11.Text = leerClientes("id_cte").Trim();
//Nombre
textBox1.Text = leerClientes("nombre").Trim();
//RFC
textBox2.Text = leerClientes("rfc").Trim();
//Tel 1
textBox3.Text = leerClientes("tel1").Trim();
//Tel 2
textBox4.Text = leerClientes("tel2").Trim();
//Calle
textBox5.Text = leerClientes("calle").Trim();
//Num_ext
textBox7.Text = leerClientes("num_ext").Trim();
//Num_int
textBox6.Text = leerClientes("num_int").Trim();
//CP
textBox8.Text = leerClientes("cp").Trim();
//Colonia
textBox9.Text = leerClientes("colonia").Trim();
//Ciudad
textBox10.Text = leerClientes("ciudad").Trim();
//Estado
textBox12.Text = leerClientes("estado").Trim();
//Pais
textBox13.Text = leerClientes("pais").Trim();
}
else
{
MessageBox.Show("El id especificado no existe");
}
/*
SqlCommand ORDEN13 = new SqlCommand("SELECT pais from
clientes where id_cte= '" + textBox14.Text + "'", con);
ORDEN13.Connection.Open(); ORDEN13.ExecuteNonQuery();
SqlDataReader reader13 = ORDEN13.ExecuteReader();
reader13.Read();
try { if (reader13.HasRows) { textBox13.Text =
reader13.GetString(0); } }
catch (Exception) { textBox13.Text = ""; }
reader13.Close(); ORDEN13.Connection.Close();
*/
}
private void llenarComboBox()
{
SqlConnection conexion = new SqlConnection();
SqlCommand comando = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet data = new DataSet();
try
{
conexion.ConnectionString = Global.cadena;
conexion.Open();
comando.Connection = conexion;
adapter.SelectCommand = comando;
comando.CommandText = "select id_cte from clientes order
by id_cte"; //el comando que llena el Combo
comando.ExecuteNonQuery();
adapter.Fill(data); //llenamos nuestro dataset
comboBox1.DataSource = data.Tables[0]; //llenamos nuestro
combobox con el dataset
comboBox1.DisplayMember =
data.Tables[0].Columns[0].Caption; //Lo mostramos
}
catch (Exception)
{
MessageBox.Show("Error");
}
}
private void limpiar()
{
comboBox1.Text = "";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
textBox10.Text = "";
textBox11.Text = "";
textBox12.Text = "";
textBox13.Text = "";
}
}
}
Descargar