Friday, 20 January 2012

ADO .NET ENTITY DATA MODEL

Right click ur project add new item
Right click ur project add new item
Then go to the database
after that u go to solution and double click on model.edmx
then right click on model
select update model from database

then clikck on finish buttom
then module window like this
then go to design view
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>













  • EID
  • :
  • NAME
  • :
  •  
  •  
  •  
  •  
  •  




















then go to code view
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
#region
DatabaseModel.DatabaseEntities o = new DatabaseModel.DatabaseEntities();
int i;
string s;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fill();

}
void fill()
{
var m = from x in o.EMPs select x;
gv.DataSource = m;
gv.DataBind();
}
protected void btn_Click(object sender, EventArgs e)
{
DatabaseModel.EMP p = new DatabaseModel.EMP();
p.ID = Convert.ToInt32(tb.Text);
p.NAME = tb1.Text;
o.AddToEMPs(p);
o.SaveChanges();
lb.Text = "One record saved.";
fill();
}
//protected void lbt_Click(object sender, EventArgs e)
//{
// LinkButton lk = (LinkButton)sender;
// GridViewRow gr = (GridViewRow)lk.NamingContainer;



//}
protected void lbt1_Click(object sender, EventArgs e)
{

}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
s = ((LinkButton)gv.Rows[e.NewSelectedIndex].FindControl("lbt")).CommandArgument.ToString();
string[] ss = s.Split('*');
tb.Text = ss[0];
tb1.Text = ss[1];
}
protected void btn1_Click(object sender, EventArgs e)
{
i=Convert.ToInt32(tb.Text);
var n = (from y in o.EMPs where y.ID == i select y).First();
n.NAME = tb1.Text;
o.SaveChanges();
lb.Text = "One record updated.";
fill();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
i = Convert.ToInt32(((LinkButton)gv.Rows[e.RowIndex].FindControl("lbt1")).CommandArgument);
var p = (from z in o.EMPs where z.ID == i select z).First();
o.EMPs.DeleteObject(p);
o.SaveChanges();
lb.Text = "One record deleted";
fill();
}
}

No comments:

Post a Comment