Professional Documents
Culture Documents
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Descripcin breve de Clientes
/// </summary>
public class Clientes
{
public int idCliente{get;set;}
public string nombreCliente { get; set; }
public string apellidosCliente { get; set; }
public string direccionCliente { get; set; }
private String cs =
ConfigurationManager.ConnectionStrings["db"].ConnectionString;
public Clientes()
{
}
public DataSet listadoClientes()
{
DataSet dt = new DataSet();
try
{
using (SqlConnection con = new SqlConnection(cs))
{
SqlDataAdapter da = new SqlDataAdapter("Select * from Clientes", con);
da.Fill(dt, "Clientes");
return dt;
}
}
catch (Exception)
{
return dt;
}
}
public bool agregarCliente(Clientes nuevoCliente) {
try
{
using(SqlConnection con= new SqlConnection(cs)){
SqlCommand comm = new SqlCommand("insert into clientes
(nombre,apellidos,direccion)
values(@nombre,@apellidos,@direccion)",con);
SqlParameter Nombre = new SqlParameter();
Nombre.ParameterName = "@nombre";
Nombre.SqlDbType = SqlDbType.VarChar;
Nombre.Value = nuevoCliente.nombreCliente;
comm.Parameters.Add(Nombre);
SqlParameter Apellidos = new SqlParameter();
Apellidos.ParameterName = "@apellidos";
Apellidos.SqlDbType = SqlDbType.VarChar;
Apellidos.Value = nuevoCliente.apellidosCliente;
comm.Parameters.Add(Apellidos);
SqlParameter Direccion = new SqlParameter();
Direccion.ParameterName = "@direccion";
Direccion.SqlDbType = SqlDbType.VarChar;
Direccion.Value = nuevoCliente.direccionCliente;
comm.Parameters.Add(Direccion);
con.Open();
comm.ExecuteNonQuery();
con.Close();
return true;
}
}
catch (Exception)
{
return false;
}
}
public bool modificarCliente(Clientes actualizaCliente) {
try
{
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand comm = new SqlCommand("update clientes set
nombre=@nombre,apellidos=@apellidos,direccion=@direccion where
idCliente=@id", con);
SqlParameter Nombre = new SqlParameter();
Nombre.ParameterName = "@nombre";
Nombre.SqlDbType = SqlDbType.VarChar;
Nombre.Value = actualizaCliente.nombreCliente;
comm.Parameters.Add(Nombre);
SqlParameter Apellidos = new SqlParameter();
Apellidos.ParameterName = "@apellidos";
Apellidos.SqlDbType = SqlDbType.VarChar;
Apellidos.Value = actualizaCliente.apellidosCliente;
comm.Parameters.Add(Apellidos);
SqlParameter Direccion = new SqlParameter();
Direccion.ParameterName = "@direccion";
Direccion.SqlDbType = SqlDbType.VarChar;
Direccion.Value = actualizaCliente.direccionCliente;
comm.Parameters.Add(Direccion);
SqlParameter Id = new SqlParameter();
Id.ParameterName = "@id";
Id.SqlDbType = SqlDbType.Int;
Id.Value = actualizaCliente.idCliente;
comm.Parameters.Add(Id);
con.Open();
comm.ExecuteNonQuery();
con.Close();
return true;
}
}
catch (Exception)
{
return false;
}
}
public bool eliminarCliente(int id) {
try
{
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand comm = new SqlCommand("delete from clientes where
idCliente=@id", con);
SqlParameter Id = new SqlParameter();
Id.ParameterName = "@id";
Id.SqlDbType = SqlDbType.Int;
Id.Value = id;
comm.Parameters.Add(Id);
con.Open();
comm.ExecuteNonQuery();
con.Close();
return true;
}
}
catch (Exception)
{
return false;
}
}
}
9.-Asta este momento ya funciona ,ahora lo que aremos es agregarle una paginacion ,
para ello agregaremos el evento PageIndexChanging y los
atributos PageSize=2 , AllowPaging=true a nuestro gridview,tambien gregaremo
el evento de sorting y modificaremos el evento de PageIndexChanging, ademas le
}
protected void gvClientes_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
gvClientes.PageIndex = e.NewPageIndex;
gvClientes.DataSource = cliente.listadoClientes();
gvClientes.DataBind();
}