Вы находитесь на странице: 1из 6

Ejemplo de ejercicio con Dataset sin tipo.

Código de formulario:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"


Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 45%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td>
Codigo de Articulo</td>
<td>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="Buscar" runat="server"
onclick="Buscar_Click" Text="Buscar" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None" Height="277px" Width="402px">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333"
/>
<FooterStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="#FFCC66"
ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-
Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>

Código Subyacente:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
DataSet dspedidos = new DataSet();
string conexion = "Data source=(local);initial
catalog=pedidoplus;integrated security=true";

protected void Buscar_Click(object sender, EventArgs e)


{
SqlDataAdapter adarticulo = new
SqlDataAdapter("select * from articulo where
artcodigo=@artcodigo",conexion);
adarticulo.SelectCommand.Parameters.Add(
"@artcodigo",SqlDbType.Int).Value =
Convert.ToInt32(this.TextBox1.Text);
adarticulo.Fill(dspedidos,"articulo");
this.GridView1.DataSource=dspedidos.Tables["articulo"];
this.GridView1.DataBind();

}
protected void Buscar_Click1(object sender, EventArgs e)
{

}
}

Ejemplo utilizando Dataset con tipo

Nombre de la
Nombre del Método que ejecutara la Tabla
sentencia Select sin paramentros

Nombre del Método


que Ejecutara la
sentencia Select con
paramentros

Nombre del TableAdapter


Codigo de formulario:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 45%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td>
Codigo de Articulo</td>
<td>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
<td>
<asp:Button ID="Buscar" runat="server"
onclick="Buscar_Click" Text="Buscar" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None" Height="277px" Width="402px">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333"
/>
<FooterStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<PagerStyle BackColor="#FFCC66"
ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-
Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>

Codigo subyacente:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
dspedidos2 dspedidos = new dspedidos2();

protected void Buscar_Click(object sender, EventArgs e)


{
int codigo = 0;
this.Label1.Text = "";
try
{
codigo = Convert.ToInt32(this.TextBox1.Text);
}
catch (Exception ex)
{
if (this.TextBox1.Text == "")
{
llenargridview();
}
else
{
codigo = 0;
}
}

dspedidos2TableAdapters.ArticuloTableAdapter adarticulo =
new dspedidos2TableAdapters.ArticuloTableAdapter();
adarticulo.FillByartcodigo(dspedidos.Articulo, codigo);
if (dspedidos.Articulo.Rows.Count == 0)
{
this.Label1.Text = "Codigo no Existe";
}
this.GridView1.DataSource = dspedidos.Articulo;
this.GridView1.DataBind();

protected void Page_Load(object sender, EventArgs e)


{
if (!Page.IsPostBack)
{
llenargridview();
}
}
private void llenargridview()
{
dspedidos2TableAdapters.ArticuloTableAdapter adarticulo =
new dspedidos2TableAdapters.ArticuloTableAdapter();
adarticulo.Fill(dspedidos.Articulo);
this.GridView1.DataSource = dspedidos.Articulo;
this.GridView1.DataBind();
}
}

Вам также может понравиться