Código de Clientes – Altas [ Dar de alta registros con SQL desde C#]

Anuncio
Código de Clientes - Altas
Esta ventana compone el registro de los clientes directamente a la Base de Datos
“Negocio”
Funciones:
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 Form5 : Form
{
public Form5()
{
InitializeComponent();
}
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("Añadir el nuevo registro a la Base
de Datos?", "Añadir nuevo cliente", MessageBoxButtons.YesNo) ==
DialogResult.Yes)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = Global.cadena;
try
{
string q = "insert into
clientes(id_cte,nombre,rfc,tel1,tel2,calle,num_ext,num_int,cp,colonia,ciu
dad,estado,pais)
values(@id_cte,@nombre,@rfc,@tel1,@tel2,@calle,@num_ext,@num_int,@cp,@col
onia,@ciudad,@estado,@pais)";
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("Nuevo registro agregado a la
Base de Datos");
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)
{
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: " + (numRegistros1).ToString();
//lleno el textbox del ID
string numero="";
if (numRegistros < 10 && numRegistros > 0) { numero =
"00" + numRegistros.ToString(); }
if (numRegistros < 100 && numRegistros >= 10) { numero =
"0" + numRegistros.ToString(); }
if (numRegistros < 1000 && numRegistros >= 100) { numero
= numRegistros.ToString(); }
textBox11.Text = "CTE" + numero.ToString()+""+System.DateTime.Now.Millisecond.ToString();
}
catch (Exception)
{
MessageBox.Show("Conexión fallida");
this.Close();
}
}
}
}
Descargar