Professional Documents
Culture Documents
GUIA DE LABORATORIO
DEMO ENTITY
1. AGREGAR UN PROYECTO C TIPO WEB
DEMO1 grilla
{
GridView1.DataSource = db.Customers;
GridView1.DataBind();
}
2Botón
MOSTRAR 2 CAMPOS
AGREGAR UN BOTON Y EL CODIGO
protected void Button1_Click(object sender, EventArgs e)
{
var R = from p in db.Customers
select new
{
p.CustomerID,
p.CompanyName
};
TextBox1.Text = R.First().CustomerID.ToString();
GridView1.DataSource = R;
GridView1.DataBind();
}
Formulario 2
GridView1.DataBind();
}
WEBFORMCONSULTASDINAMICAS
namespace WebEntity1
{
public partial class WebForm2ConDinamicas : System.Web.UI.Page
{
NorthwindEntities db = new NorthwindEntities();
private void buscar(int cod)
{
Region r = (from p in db.Region
where p.RegionID == cod
select p).First();
txtRegionDescription.Text = (r.RegionDescription).ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
buscar(int.Parse(txtRegionID.Text));
}
}}
namespace WebEntity1
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// enlazando dropdownlist
using (NorthwindEntities db = new NorthwindEntities())
{
ddlNewEmployee.DataSource = (from emp in db.Employees
Pág.# 3 Mg. Gina Huertas Camacho
U.D “Taller de Programación Web”
ddlNewEmployee.DataTextField = "EmployeeID";
ddlNewEmployee.DataValueField = "LastName";
ddlNewEmployee.DataBind();
}
// enlazando Listbox
using (var context = new NorthwindEntities())
{
var emp = from p in context.Employees
where p.LastName == "Davolio"
select p;
foreach (var person in emp)
{
ListBox1.Items.Add(string.Format("{0} {1}", person.FirstName, person.LastName));
}
}
}
}
}
namespace demoEntity
{
public partial class WebForm1 : System.Web.UI.Page
{
private NorthwindEntities db =new NorthwindEntities();
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = db.Customers;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Customer c = new Customer();
c.CustomerID = TextBox1.Text;
c.CompanyName = TextBox2.Text;
db.AddToCustomers(c);
db.SaveChanges();
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.DataSource = db.Customers.Where(em => em.CustomerID ==
TextBox1.Text);
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
GridView1.DataSource = db.Customers;
GridView1.DataBind();
}
protected void Button5_Click(object sender, EventArgs e)
{
//int s = Convert.ToInt32(TextBox1.Text); si fuera el codigo entero
Customer c = db.Customers.Where(emt => emt.CustomerID ==
TextBox1.Text).First() ;
db.DeleteObject(c);
db.SaveChanges();
}
{
public partial class WebForm1 : System.Web.UI.Page
{
private NorthwindEntities db = new NorthwindEntities();
private void agregar()
{
Pág.# 7 Mg. Gina Huertas Camacho
U.D “Taller de Programación Web”
llenarDrop();
}
protected void Button4_Click(object sender, EventArgs e)
{
eliminar(int.Parse(txtRegionID.Text));
llenarDrop(); listarDatos();
} }}
WEBFORM REGION
ddlNewEmployee.DataTextField = "EmployeeID";
ddlNewEmployee.DataValueField = "LastName";
ddlNewEmployee.DataBind();
}
// enlazando Listbox
using (var context = new NorthwindEntities())
{
var emp = from p in context.Employees
select p.LastName ;
foreach (var person in emp)
{
ListBox1.Items.Add(person );
}
}
}
}