Friday, 20 January 2012

ADO .NET ENTITY DATA MODEL USING STORE PROCEDURE

Right click ur project add new item
Right clion ur project add new item
Then go to the database
Here i have created three procedures from dml operation that procedure are
PROCEDURE FOR INSERT
ALTER PROCEDURE emp1
(
@NAME VARCHAR(50)
)

AS
BEGIN
SET NOCOUNT ON
INSERT INTO EMP(NAME) VALUES(@NAME)
SET NOCOUNT OFF
END
PROCEDURE FOR UPDATE
CREATE PROCEDURE emp2
(
@ID INT,
@NAME VARCHAR(50)
)
PROCEDURE FOR DELETE
CREATE PROCEDURE emp3
(
@ID INT
)

AS
BEGIN
SET NOCOUNT ON
DELETE FROM EMP WHERE ID=@ID
SET NOCOUNT OFF
END
THEN MAPPING THE PROCEDURE GO TO SOLUTION DOUBLE CLICK ON THE Model.edmx, GO TO model window then right click on the table then select procedure mapping


AS
BEGIN
SET NOCOUNT ON
UPDATE EMP SET NAME=@NAME WHERE ID=@ID
SET NOCOUNT OFF
END
after that u go to solution and double click on model.edmx
then right click on model
select update model from database
then right click on model
select update model from database
after that rihght click on table select store procedure mapping
then select the procedure name from the drop down

right click on the model window select model brower
expand the procedure folder select then right click on procedure select add function import
then new window open after that click on ok button








then clikck on finish buttom
then module window clike this
then go to design view
after that u 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)
{
o.emp1(tb1.Text);
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);
o.emp2(i, 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);
o.emp3(i);
o.SaveChanges();
lb.Text = "One record deleted";
fill();
}
}

No comments:

Post a Comment