Entity Framework CRUD
In this article I will explain with an example, how to perform select, insert, edit, update, delete using Entity Framework in ASP.Net using C#
Database:
I have made use of the following table Sample with the schema as follows
Configuring and connecting Entity Framework to database
Now I will explain the steps to configure and add Entity Framework and also how to connect it with the database.You will need to add Entity Data Model to your project using Add New Item Dialog as shown below.
As soon as you add the Entity Data Model to your project you will be prompted with the following dialog. You need to click YES button.
Then the Entity Data Model Wizard will open up where you need to select Generate from database option
Now the wizard will ask you to connect and configure the connection string to the database.
Next you will need to choose the Tables you need to connect and work with Entity Framework. Since I need to work on the Customers Table I have checked that.
The above was the last step and you should now have the Entity Data Model ready with the Customers Table.
Now we will create different pages for Create ,Update ,select(Read) And Delete Operations.
1.Create records:
HTML Markup:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Contact"></asp:Label>
<asp:TextBox ID="txt_contact" runat="server"></asp:TextBox>
<asp:Button ID="btnsubmit" runat="server" OnClick="btnsubmit_Click" Text="Submit" />
<asp:Label ID="lblmsg" Visible="false" runat="server" Text="Data saved"></asp:Label></div>
</form>
</body>
</html>
C# Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class create : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
demoEntities db = new demoEntities();
protected void btnsubmit_Click(object sender, EventArgs e)
{
sample sm = new sample();
sm.name = txt_name.Text;
sm.contact = txt_contact.Text;
db.samples.Add(sm);
db.SaveChanges();
lblmsg.Visible = true;
}
}
Output :
HTML Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="select.aspx.cs" Inherits="select" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnshow" runat="server" OnClick="btnshow_Click" Text="Show" />
<br />
<asp:Label ID="lblid" runat="server" Text=""></asp:Label>
<br />
<asp:Label ID="lblname" runat="server" Text=""></asp:Label>
<br />
<asp:Label ID="lblcontact" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="btnshowall" runat="server" OnClick="btnshowall_Click" Text="Show All" />
<asp:GridView ID="gvall" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
C# Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class select : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
demoEntities db = new demoEntities();
protected void btnshow_Click(object sender, EventArgs e)
{
int id = int.Parse(txtid.Text);
var qur = from abc in db.samples where abc.id == id select abc;
sample sa = qur.SingleOrDefault();
lblid.Text = sa.id.ToString();
lblname.Text = sa.name;
lblcontact.Text = sa.contact;
}
protected void btnshowall_Click(object sender, EventArgs e)
{
var qur = from abc in db.samples select abc;
sample[] sa = qur.ToArray();
gvall.DataSource = sa;
gvall.DataBind();
}
}
Output :
3.Update records:
HTML Markup:
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class uprecord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
demoEntities db = new demoEntities();
protected void btnupdate_Click(object sender, EventArgs e)
{
int id = int.Parse(txtid.Text);
var qur = from abc in db.samples where abc.id == id select abc;
sample sa = qur.SingleOrDefault();
sa.name = txtname.Text;
sa.contact = txtcontact.Text;
db.SaveChanges();
}
protected void btnshow_Click(object sender, EventArgs e)
{
int id = int.Parse(txtid.Text);
var qur = from abc in db.samples where abc.id == id select abc;
sample sa = qur.SingleOrDefault();
txtname.Text = sa.name;
txtcontact.Text = sa.contact;
}
protected void btnshowall_Click(object sender, EventArgs e)
{
var qur = from abc in db.samples select abc;
sample[] sa = qur.ToArray();
gvall.DataSource = sa;
gvall.DataBind();
}
}
Output:
4.Delete Records:
HTML Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="delrec.aspx.cs" Inherits="delrec" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btndel" runat="server" OnClick="btndel_Click" Text="Delete" />
<asp:Button ID="btnshowall" runat="server" OnClick="btnshowall_Click" Text="Show All" />
<asp:GridView ID="gvall" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Output :
HTML Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="uprecord.aspx.cs" Inherits="uprecord" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnshow" runat="server" OnClick="btnshow_Click" Text="Show" />
<br />
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
<asp:TextBox ID="txtcontact" runat="server"></asp:TextBox><br />
<asp:Button ID="btnupdate" runat="server" OnClick="btnupdate_Click" Text="Update" />
<br />
<asp:Button ID="btnshowall" runat="server" OnClick="btnshowall_Click" Text="Show All" />
<asp:GridView ID="gvall" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class uprecord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
demoEntities db = new demoEntities();
protected void btnupdate_Click(object sender, EventArgs e)
{
int id = int.Parse(txtid.Text);
var qur = from abc in db.samples where abc.id == id select abc;
sample sa = qur.SingleOrDefault();
sa.name = txtname.Text;
sa.contact = txtcontact.Text;
db.SaveChanges();
}
protected void btnshow_Click(object sender, EventArgs e)
{
int id = int.Parse(txtid.Text);
var qur = from abc in db.samples where abc.id == id select abc;
sample sa = qur.SingleOrDefault();
txtname.Text = sa.name;
txtcontact.Text = sa.contact;
}
protected void btnshowall_Click(object sender, EventArgs e)
{
var qur = from abc in db.samples select abc;
sample[] sa = qur.ToArray();
gvall.DataSource = sa;
gvall.DataBind();
}
}
Output:
4.Delete Records:
HTML Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="delrec.aspx.cs" Inherits="delrec" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btndel" runat="server" OnClick="btndel_Click" Text="Delete" />
<asp:Button ID="btnshowall" runat="server" OnClick="btnshowall_Click" Text="Show All" />
<asp:GridView ID="gvall" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class delrec : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
demoEntities db = new demoEntities();
protected void btndel_Click(object sender, EventArgs e)
{
int id = int.Parse(txtid.Text);
var qur = from abc in db.samples where abc.id == id select abc;
sample sa = qur.SingleOrDefault();
db.samples.Remove(sa);
db.SaveChanges();
}
protected void btnshowall_Click(object sender, EventArgs e)
{
var qur = from abc in db.samples select abc;
sample[] sa = qur.ToArray();
gvall.DataSource = sa;
gvall.DataBind();
}
}
Output:
Download Source code :
Google Drive
Google Drive
Thank you for this tutorial
ReplyDeleteI am new to Entity Framework that will be help me.