Entity Framework 4.0 Insert Update and Delete
For small blogs, it requires more references and explanation, which sometimes are redundant. So I thought of writing single blog which is combination of topics more or less related to one thing.
I am exploring ADO.NET Entity Framework and have been trying things out there. Here I am planning to demonstrate how to do insert, update and delete.
STEP 2:
CSS
<style>
.gvdata th
{
background-color:#3A4F63;
color:White;
padding:10px;
}
.gvdata
{
text-align:center;
}
.leftCol
{
width:200px;
text-align:left;
padding:5px;
}
</style>
STEP 3:
ASPX
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<p>
<span class="leftCol">CountryID</span><span><asp:TextBox ID="txtCountryID" runat="server"/></span><br />
<span class="leftCol">Country Name</span><span><asp:TextBox ID="txtsName" runat="server"/></span><br />
<span class="leftCol"><asp:Button ID="btnSave" Text="Save" runat="server"
onclick="btnSave_Click" /></span>
<asp:HiddenField runat="server" ID="HdID" />
<asp:GridView ID="gvdata" CssClass="gvdata" runat="server" AutoGenerateColumns="false"
onrowcommand="gvdata_RowCommand" onrowdeleting="gvdata_RowDeleting"
onrowediting="gvdata_RowEditing">
<Columns>
<asp:BoundField HeaderText="CountryID" DataField="nCountryID" />
<asp:BoundField HeaderText="Country Name" DataField="sName" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate >
<asp:LinkButton ID="lnq" runat="server" CommandName="Edit" CommandArgument='<%#Eval("nID") %>'> Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" CommandArgument='<%#Eval("nID") %>'> Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</p>
STEP 4 :
Codebehind aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fillGrid();
}
public void fillGrid()
{
testEntities dbmocktest = new testEntities();
var data = (from c in dbmocktest.tbl_State select c).ToList();
gvdata.DataSource = data;
gvdata.DataBind();
}
public void DoEmpty()
{
txtsName.Text = "";
txtCountryID.Text = "";
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
using testEntities dbmocktest = newtestEntities())
{
tbl_State data = new tbl_State();
// var data = (from c in dbmocktest.tbl_State select c).First();
data.nCountryID = Convert.ToInt32(txtCountryID.Text);
data.sName = txtsName.Text;
if (btnSave.Text == "Save")
{
// var data = (from c in dbmocktest.tbl_State select c).First();
// var data = (from c in dbmocktest.tbl_State select c).First();
dbmocktest.AddTotbl_State(data);
}
else if (btnSave.Text == "Update")
{
Int32 ID = Convert.ToInt32(HdID.Value);
data = (from c in dbmocktest.tbl_State where c.nID==ID select c).First();
btnSave.Text = "Save";
}
data.nCountryID = Convert.ToInt32(txtCountryID.Text);
data.sName = txtsName.Text;
dbmocktest.SaveChanges();
}
}
finally {
fillGrid();
DoEmpty();
}
}
protected void gvdata_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void gvdata_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void gvdata_RowCommand(object sender, GridViewCommandEventArgs e)
{
Int32 nID = Convert.ToInt32(e.CommandArgument);
using (testEntities dbmocktest = new testEntities())
{
var data = (from c in dbmocktest.tbl_State where c.nID == nID select c).First();
if (e.CommandName == "Edit")
{
// tbl_State data = new tbl_State();
txtCountryID.Text= data.nCountryID.ToString();// = Convert.ToInt32();
HdID.Value = data.nID.ToString();
txtsName.Text= data.sName;
btnSave.Text = "Update";
}
else if (e.CommandName == "Delete")
{
dbmocktest.tbl_State.DeleteObject(data);
dbmocktest.SaveChanges();
fillGrid();
}
}
}
HOPE IT WORKS
THANKS
REHAN PARVEZ