You are on page 1of 5

using System;

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;
}
}
}

8.-Ahora agregaremos la conexion a la base de datos , para ello agregaremos la cadena


de conexion en el web config
<connectionStrings> <add name=db connectionString=Data
Source=(local);user=sa;password=password;initial
catalog=WordpressEjemplos/> </connectionStrings>

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

agregaremos las siguiente propiedad al gridview AllowSorting=true,generaremos un


metodo que nos ordene los datos

protected void gvClientes_PageIndexChanging(object sender,


GridViewPageEventArgs e)
{
string columna;
string orden;
if(Session["columna"]!=null){
columna = Session["columna"].ToString();
if (Session["orden"] != null && Session["orden"].ToString() ==
SortDirection.Ascending.ToString())
{
orden = ASC;
}
else {
orden = DESC;
}
ordena(columna,orden);
}
gvClientes.PageIndex = e.NewPageIndex;
gvClientes.DataSource = cliente.listadoClientes();
gvClientes.DataBind();
}
protected void gvClientes_Sorting(object sender, GridViewSortEventArgs
e)
{
Session["columna"] = e.SortExpression;
if (Session["orden"] != null && Session["orden"].ToString() ==
SortDirection.Descending.ToString())
{
Session["orden"] = SortDirection.Ascending;
ordena(e.SortExpression, ASC);
}
else {
Session["orden"] = SortDirection.Descending;
ordena(e.SortExpression, DESC);
}
}
private void ordena(string columna ,string orden) {
DataSet ds = cliente.listadoClientes();
DataView dv = new DataView(ds.Tables["Clientes"]);
dv.Sort = columna + + orden;
gvClientes.DataSource = dv;
gvClientes.DataBind();

}
protected void gvClientes_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
gvClientes.PageIndex = e.NewPageIndex;
gvClientes.DataSource = cliente.listadoClientes();
gvClientes.DataBind();
}

<asp:GridView runat=server ID=gvClientes


AutoGenerateColumns=False
OnRowCancelingEdit=gvClientes_RowCancelingEdit
OnRowEditing=gvClientes_RowEditing
OnRowUpdating=gvClientes_RowUpdating
PageSize=2 AllowPaging=true
OnPageIndexChanging=gvClientes_PageIndexChanging
AllowSorting=true OnSorting=gvClientes_Sorting
>
<Columns>
<asp:CommandField ShowEditButton=True />
<asp:BoundField DataField=idCliente SortExpression=ID
HeaderText=ID />
<asp:BoundField DataField=nombre SortExpression=Nombre
HeaderText=Nombre />
<asp:BoundField DataField=apellidos SortExpression=Apellidos
HeaderText=Apellidos />
<asp:TemplateField HeaderText=Direccion >
<EditItemTemplate>
<asp:TextBox TextMode=MultiLine runat=server ID=txtEditDireccion
Text=<%#Eval(direccion) %>></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox runat=server ID=direccion Text=<%#Eval(direccion)
%>></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>

You might also like