Académique Documents
Professionnel Documents
Culture Documents
NET
g.
4. En el SQLSERVER restaure la base de datos suministrada por el profesor para dicho fin.
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
namespace Capa4_entidades
{
public class clsCliente
{
#region "Variables Globales, Campos o Atributos"
private int _idCliente;
private string _NombreCliente;
private string _ApellidoCliente;
private string _DireccionCliente;
private string _DNICliente;
private string _TelefonoCliente;
private string _RUCCliente;
private Boolean _EstadoCliente;
#endregion
#region "Propiedades"
public int pIdCliente
{
get {return _idCliente;}
set {_idCliente = value;}
}
public string pNombreCliente
{
get { return _NombreCliente; }
set { _NombreCliente = value; }
}
public string pApellidoCliente
{
get { return _ApellidoCliente; }
set { _ApellidoCliente = value; }
}
public string pDireccionCliente
{
get { return _DireccionCliente; }
set { _DireccionCliente = value; }
}
public string pDNICliente
{
get { return _DNICliente; }
set { _DNICliente = value; }
}
public string pTelefonoCliente
{
get { return _TelefonoCliente; }
set { _TelefonoCliente = value; }
}
public string pRUCCliente
{
get { return _RUCCliente; }
set { _RUCCliente = value; }
}
{
get { return _EstadoCliente; }
set { _EstadoCliente = value; }
}
#endregion
#region "Constructores"
public clsCliente()
{
//SUB NEW VACIO
}
public clsCliente(int idCliente,
string NombreCliente,
string ApellidoCliente,
string DireccionCliente,
string DNICliente,
string TelefonoCliente,
string RUCCliente,
bool EstadoCliente)
{
ValidaData(idCliente, NombreCliente, ApellidoCliente, DireccionCliente, DNICliente,
TelefonoCliente, RUCCliente, EstadoCliente);
SetData(idCliente, NombreCliente, ApellidoCliente, DireccionCliente, DNICliente,
TelefonoCliente, RUCCliente, EstadoCliente);
}
#endregion
#region "Metodos"
/// <summary>
/// Valida los datos del objeto
/// </summary>
/// <param name="idCliente"></param>
/// <param name="NombreCliente"></param>
/// <param name="ApellidoCliente"></param>
/// <param name="DireccionCliente"></param>
/// <param name="DNICliente"></param>
/// <param name="TelefonoCliente"></param>
/// <param name="RUCCliente"></param>
/// <param name="EstadoCliente"></param>
public void ValidaData(int idCliente,
string NombreCliente,
string ApellidoCliente,
string DireccionCliente,
string DNICliente,
string TelefonoCliente,
string RUCCliente,
bool EstadoCliente)
{
if (idCliente <= 0)
{
throw new System.ArgumentException ("El id del cliente debe ser mayor a
0", "ID Cliente");
}
if (String.IsNullOrEmpty (NombreCliente) ||
String.IsNullOrWhiteSpace(NombreCliente))
{
throw new System.ArgumentException("El valor del Nombre del Cliente debe
ser indicado", "Nombre Cliente");
}
if (String.IsNullOrEmpty(ApellidoCliente) ||
String.IsNullOrWhiteSpace(ApellidoCliente))
{
_TelefonoCliente = TelefonoCliente;
_RUCCliente = RUCCliente;
_EstadoCliente = EstadoCliente;
}
#endregion
#region "Eventos"
#endregion
}
}
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Data.SqlClient;
namespace Capa3_Datos
{
public class clsConexion
{
#region "Variables Globales, Campos o Atributos"
//private SqlConnection conexion;
string cadena = "Data Source=CFALLASD_UTC;Initial Catalog=01-SISTEMAFACTURACION;Integrated
Security=True";
#endregion
#region "Propiedades"
#endregion
#region "Constructores"
#endregion
#region "Metodos"
public SqlConnection obtenerConeccion()
{
SqlConnection conexion = new SqlConnection(cadena);
if (conexion.State == System.Data.ConnectionState.Open)
{
conexion.Close();
}
else
{
conexion.Open();
}
return conexion;
}
#endregion
#region "Eventos"
#endregion
}
}
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
Capa4_entidades;
System.Data.SqlClient;
System.Data;
namespace Capa3_Datos
{
public class clsClienteDAL
{
#region "Variables"
//SqlDataAdapter daCliente;
SqlCommand cmdCliente = new SqlCommand();
//DataSet dsCliente;
//SqlDataReader drCliente;
clsConexion objConexion = new clsConexion();
#endregion
#region "Propiedades"
#endregion
#region "Constructores"
#endregion
#region "Metodos"
public string AgregarCliente(clsCliente objClienteE)
{
string mensaje = string.Empty;
cmdCliente.CommandType = CommandType.StoredProcedure;
cmdCliente.CommandText = "SP_Insertar_Cliente";
cmdCliente.Connection = objConexion.obtenerConeccion();
cmdCliente.Parameters.Add("@idCliente", SqlDbType.Int).Value =
objClienteE.pIdCliente;
cmdCliente.Parameters.Add("@nomCliente", SqlDbType.VarChar).Value
objClienteE.pNombreCliente;
cmdCliente.Parameters.Add("@apeCliente", SqlDbType.VarChar).Value
objClienteE.pApellidoCliente;
cmdCliente.Parameters.Add("@dniCliente", SqlDbType.VarChar).Value
objClienteE.pDNICliente;
cmdCliente.Parameters.Add("@rucCliente", SqlDbType.VarChar).Value
objClienteE.pRUCCliente;
cmdCliente.Parameters.Add("@telCliente", SqlDbType.VarChar).Value
objClienteE.pTelefonoCliente;
cmdCliente.Parameters.Add("@dirCliente", SqlDbType.VarChar).Value
objClienteE.pDireccionCliente;
int registros;
registros = cmdCliente.ExecuteNonQuery();
if (registros == 1)
{
mensaje = "Registro guardado correctamente";
}
else
{
mensaje = "Error en la transaccion";
}
cmdCliente.Parameters.Clear();
objConexion.obtenerConeccion().Close();
return mensaje;
=
=
=
=
=
=
}
public string ActualizarCliente(clsCliente objClienteE)
{
string mensaje;
cmdCliente.CommandType = CommandType.StoredProcedure;
cmdCliente.CommandText = "SP_Actualizar_Cliente";
cmdCliente.Connection = objConexion.obtenerConeccion();
cmdCliente.Parameters.Add("@idCliente", SqlDbType.Int).Value =
objClienteE.pIdCliente;
cmdCliente.Parameters.Add("@nomCliente", SqlDbType.VarChar).Value =
objClienteE.pNombreCliente;
cmdCliente.Parameters.Add("@apeCliente", SqlDbType.VarChar).Value =
objClienteE.pApellidoCliente;
cmdCliente.Parameters.Add("@dniCliente", SqlDbType.VarChar).Value =
objClienteE.pDNICliente;
cmdCliente.Parameters.Add("@rucCliente", SqlDbType.VarChar).Value =
objClienteE.pRUCCliente;
cmdCliente.Parameters.Add("@telCliente", SqlDbType.VarChar).Value =
objClienteE.pTelefonoCliente;
cmdCliente.Parameters.Add("@dirCliente", SqlDbType.VarChar).Value =
objClienteE.pDireccionCliente;
int registros;
registros = cmdCliente.ExecuteNonQuery();
if (registros == 1)
{
mensaje = "Registro Actualizado correctamente";
}
else
{
mensaje = "Error en la transaccion";
}
cmdCliente.Parameters.Clear();
objConexion.obtenerConeccion().Close();
return mensaje;
}
public List<clsCliente> ListarClientes()
{
List<clsCliente> lista = new List<clsCliente>();
SqlDataReader dr;
cmdCliente.CommandType = CommandType.StoredProcedure;
cmdCliente.CommandText = "SP_Listar_Clientes";
cmdCliente.Connection = objConexion.obtenerConeccion();
dr = cmdCliente.ExecuteReader();
while (dr.Read())
{
clsCliente objCliente = new clsCliente();
objCliente.pIdCliente = Convert.ToInt16(dr.GetValue(0));
objCliente.pNombreCliente =Convert.ToString(dr.GetValue(1));
objCliente.pApellidoCliente = Convert.ToString(dr.GetValue(2));
objCliente.pDireccionCliente = Convert.ToString(dr.GetValue(6));
objCliente.pDNICliente = Convert.ToString(dr.GetValue(3));
objCliente.pTelefonoCliente = Convert.ToString(dr.GetValue(5));
objCliente.pRUCCliente = Convert.ToString(dr.GetValue(4));
lista.Add(objCliente);
}
return lista;
}
public List<clsCliente> BuscarCliente(int codigo)
{
List<clsCliente> lista = new List<clsCliente>();
SqlDataReader dr;
cmdCliente.CommandType = CommandType.StoredProcedure;
cmdCliente.CommandText = "Sp_BuscarCliente";
cmdCliente.Connection = objConexion.obtenerConeccion();
cmdCliente.Parameters.Add("@idCliente", SqlDbType.Int).Value = codigo;
dr = cmdCliente.ExecuteReader();
while (dr.Read())
{
clsCliente objCliente = new clsCliente();
objCliente.pIdCliente = Convert.ToInt16(dr.GetValue(0));
objCliente.pNombreCliente = Convert.ToString(dr.GetValue(1));
objCliente.pApellidoCliente = Convert.ToString(dr.GetValue(2));
objCliente.pDireccionCliente = Convert.ToString(dr.GetValue(6));
objCliente.pDNICliente = Convert.ToString(dr.GetValue(3));
objCliente.pTelefonoCliente = Convert.ToString(dr.GetValue(5));
objCliente.pRUCCliente = Convert.ToString(dr.GetValue(4));
lista.Add(objCliente);
}
return lista;
}
public int codCliente()
{
int codigo=0;
SqlDataReader dr;
cmdCliente.CommandType = CommandType.StoredProcedure;
cmdCliente.CommandText = "SP_Generar_Cod_Cliente";
cmdCliente.Connection = objConexion.obtenerConeccion();
dr = cmdCliente.ExecuteReader();
while (dr.Read())
{
codigo = Convert.ToInt16(dr.GetValue(0));
}
return codigo;
}
#endregion
#region "Eventos"
#endregion
}
}
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
Capa3_Datos;
Capa4_entidades;
namespace Capa2_Logica
{
public class clsClienteBLL
{
#region "Variables Globales, Campos o Atributos"
private clsClienteDAL objClienteDAO = new clsClienteDAL ();
#endregion
#region "Propiedades"
#endregion
#region "Constructores"
#endregion
#region "Metodos"
public String AgregarCliente(clsCliente objClienteE)
{
return objClienteDAO.AgregarCliente(objClienteE);
}
public string ActualizarCliente(clsCliente objClienteE)
{
return objClienteDAO.ActualizarCliente(objClienteE);
}
public int CodCliente()
{
return objClienteDAO.codCliente();
}
public List<clsCliente> listarClientes()
{
return objClienteDAO.ListarClientes();
}
public List<clsCliente> ListarCliente(int codigo)
{
return objClienteDAO.BuscarCliente(codigo);
}
#endregion
#region "Eventos"
#endregion
}
}
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
Capa2_Logica;
Capa4_entidades;
namespace Capa1_Presentacion
{
public partial class frmCliente : Form
{
#region "Variables Globales, Campos o Atributos"
#endregion
#region "Propiedades"
#endregion
#region "Constructores"
public frmCliente()
{
InitializeComponent();
}
#endregion
#region "Metodos"
void NuevoCliente()
{
//Crea Objeto sin validacion
clsCliente objClienteE = new clsCliente ();
objClienteE.pIdCliente = Convert.ToInt16 (txtCodCliente.Text);
objClienteE.pNombreCliente = txtNomCliente.Text;
objClienteE.pApellidoCliente = txtApeCliente.Text;
objClienteE.pDireccionCliente = txtDirrCliente.Text;
objClienteE.pDNICliente = txtDNICliente.Text;
objClienteE.pTelefonoCliente = txtTelfCliente.Text;
objClienteE.pRUCCliente = txtRUC.Text;
//Crea Objeto con validacion
try
{
objClienteE = new clsCliente(Convert.ToInt16 (txtCodCliente.Text),
txtNomCliente.Text,
txtApeCliente.Text,
txtDirrCliente.Text,
txtDNICliente.Text,
txtTelfCliente.Text,
txtRUC.Text, false);
clsClienteBLL objClienteNE = new clsClienteBLL();
string salida = objClienteNE.AgregarCliente(objClienteE);
MessageBox.Show(salida);
dgvCliente.DataSource = objClienteNE.listarClientes();
habilitarBotones(true, false, false, false);
HabilitarCajas(false);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void LimpiarCajas()
{
txtCodCliente.Clear();
txtNomCliente.Clear();
txtApeCliente.Clear();
txtDirrCliente.Clear();
txtDNICliente.Clear();
txtTelfCliente.Clear();
txtRUC.Clear();
}
void HabilitarCajas(bool estado)
{
txtCodCliente.Enabled = false;
txtNomCliente.Enabled = estado;
txtApeCliente.Enabled = estado;
txtDirrCliente.Enabled = estado;
txtDNICliente.Enabled = estado;
txtTelfCliente.Enabled = estado;
txtRUC.Enabled = estado;
txtNomCliente.Focus();
}
void ConfigurarDataGrid()
{
dgvCliente.RowsDefaultCellStyle.BackColor = Color.LightYellow;
dgvCliente.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGreen;
}
void habilitarBotones(bool Nuevo, bool Agregar, bool Actualizar, bool Cancelar)
{
btnNuevo.Enabled = Nuevo;
btnGrabar.Enabled = Agregar;
btnActualizar.Enabled = Actualizar;
btnCancelar.Enabled = Cancelar;
}
void BuscarClientes(int codigo)
{
}
private void btnActualizar_Click(object sender, EventArgs e)
{
actualizarClientes();
}
private void btnGrabar_Click(object sender, EventArgs e)
{
NuevoCliente();
}
private void dgvCliente_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvCliente.Rows.Count > 0)
{
DataGridViewRow dgvFila = dgvCliente.CurrentRow;
txtCodCliente.Text = dgvFila.Cells[0].Value.ToString();
txtNomCliente.Text = dgvFila.Cells[1].Value.ToString();
txtApeCliente.Text = dgvFila.Cells[2].Value.ToString();
txtDirrCliente.Text = dgvFila.Cells[3].Value.ToString();
txtDNICliente.Text = dgvFila.Cells[4].Value.ToString();
txtTelfCliente.Text = dgvFila.Cells[5].Value.ToString();
txtRUC.Text = dgvFila.Cells[6].Value.ToString();
}
else
{
MessageBox.Show("No hay ningun registro seleccionado");
}
habilitarBotones(false, false, true, true);
HabilitarCajas(true);
}
#endregion
}
}