Sunday, 1 January 2012

Creating an ADO.NET Entity Framework Entity Data Model

press me
press here
design page:

Code for dml operation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;

public partial class _Default : System.Web.UI.Page
{
#region
DatabaseEntities o = new DatabaseEntities();
#endregion
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
view();
}
void view()
{
gv.DataSource = o.EMPs;
gv.DataBind();
}
protected void btn_Click(object sender, EventArgs e)
{
EMP p = new EMP();
p.EID =Convert.ToInt32( tb.Text);
p.NAME = tb1.Text;
p.DID = Convert.ToInt32(tb2.Text);
o.AddToEMPs(p);
o.SaveChanges();
view();
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
view();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var i = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
var m = (from x in o.EMPs
where x.EID == i
select x).First();
o.EMPs.DeleteObject(m);
o.SaveChanges();
view();
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var i=Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
var m = (from x in o.EMPs
where x.EID == i
select x).First();
m.NAME = ((TextBox)gv.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
o.SaveChanges();
gv.EditIndex = -1;
view();

}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
view();
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
view();
}
}

No comments:

Post a Comment