This article shows how to do create, Retrieve, Update, Delete using Entity Framework in ASP.NET web application. Follow these below steps:
CREATE TABLE [dbo].[Tbl_Student] (
[Student_Id] INT IDENTITY (1, 1) NOT NULL,
[Student_Name] VARCHAR (50) NULL,
[Student_Age] INT NULL,
[Student_Address] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);
Now create the one sample application "Demo_Asp_DotNet" as:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 37%;
height: 50px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="auto-style1" style="border-style: solid">
<tr>
<td style="width: 50%; text-align: right;">Search By Id : </td>
<td style="width: 50%">
<asp:TextBox ID="txtid" runat="server" AutoPostBack="true" OnTextChanged="txtid_TextChanged"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;"> </td>
<td style="width: 50%"> </td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">Name : </td>
<td style="width: 50%">
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">Age : </td>
<td style="width: 50%">
<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">Address : </td>
<td style="width: 50%">
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">
<asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="Save" Width="72px" />
</td>
<td style="width: 50%">
<asp:Button ID="btnreset" runat="server" OnClick="btnreset_Click" Text="Reset" Width="72px" />
</td>
</tr>
</table>
</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;
using Demo_Asp_DotNet.Models;
using System.Data.Entity;
namespace Demo_Asp_DotNet
{
public partial class cRUD_ASP : System.Web.UI.Page
{
Database1Entities OB = new Database1Entities();
protected void Page_Load(object sender, EventArgs e)
{
}
public void clear()
{
txtaddress.Text = "";
txtage.Text = "";
txtname.Text = "";
txtid.Text = "";
}
protected void btnsave_Click(object sender, EventArgs e)
{
if (btnsave.Text == "Save")
{
Tbl_Student ON = new Tbl_Student();
ON.Student_Name = txtname.Text;
ON.Student_Age = Convert.ToInt32(txtage.Text);
ON.Student_Address = txtaddress.Text;
OB.Tbl_Student.Add(ON);
OB.SaveChanges();
Response.Write("<script>alert('Successfully Saved...')</script>");
clear();
}
else if(btnsave.Text=="Update")
{
int ID = Convert.ToInt32(txtid.Text);
Tbl_Student ON = OB.Tbl_Student.Where(b=>b.Student_Id==ID).FirstOrDefault();
ON.Student_Name = txtname.Text;
ON.Student_Age = Convert.ToInt32(txtage.Text);
ON.Student_Address = txtaddress.Text;
OB.SaveChanges();
Response.Write("<script>alert('Successfully Updated...')</script>");
clear();
btnsave.Text = "Save";
btnreset.Text = "Reset";
}
}
protected void btnreset_Click(object sender, EventArgs e)
{
if (btnreset.Text == "Reset")
{
clear();
}
else if(btnreset.Text=="Delete")
{
int ID = Convert.ToInt32(txtid.Text);
Tbl_Student ON = OB.Tbl_Student.Where(b => b.Student_Id == ID).FirstOrDefault();
OB.Tbl_Student.Remove(ON);
OB.SaveChanges();
Response.Write("<script>alert('Successfully Deleted...')</script>");
clear();
}
}
protected void txtid_TextChanged(object sender, EventArgs e)
{
int ID=Convert.ToInt32(txtid.Text);
Tbl_Student ON = OB.Tbl_Student.Where(b => b.Student_Id == ID).FirstOrDefault();
txtname.Text = ON.Student_Name;
txtage.Text = ON.Student_Age.ToString();
txtaddress.Text = ON.Student_Address;
btnsave.Text = "Update";
btnreset.Text = "Delete";
}
}
}
Step-1: Create Table
First create a table "dbo.Tbl_Student" in database.CREATE TABLE [dbo].[Tbl_Student] (
[Student_Id] INT IDENTITY (1, 1) NOT NULL,
[Student_Name] VARCHAR (50) NULL,
[Student_Age] INT NULL,
[Student_Address] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);
Step-2: Create a new ASP.Net web application
Now create the one sample application "Demo_Asp_DotNet" as:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page).
- Provide the web site a name such as "Demo_Asp_DotNet" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - "cRUD_ASP.aspx page".
- Drag and drop required controls on the <form> section of the cRUD_ASP.aspx page.
Step-3: Add Entity Data Model in Solution
Then add entity data model in model folder.Step-4: Write Code in Design Page
Write this below code in "cRUD_ASP.aspx" design page.<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 37%;
height: 50px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="auto-style1" style="border-style: solid">
<tr>
<td style="width: 50%; text-align: right;">Search By Id : </td>
<td style="width: 50%">
<asp:TextBox ID="txtid" runat="server" AutoPostBack="true" OnTextChanged="txtid_TextChanged"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;"> </td>
<td style="width: 50%"> </td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">Name : </td>
<td style="width: 50%">
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">Age : </td>
<td style="width: 50%">
<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">Address : </td>
<td style="width: 50%">
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 50%; text-align: right;">
<asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="Save" Width="72px" />
</td>
<td style="width: 50%">
<asp:Button ID="btnreset" runat="server" OnClick="btnreset_Click" Text="Reset" Width="72px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step-5: Write Code in .cs page or Back-end Page
Write this below code in "cRUD_ASP.aspx.cs" page.using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo_Asp_DotNet.Models;
using System.Data.Entity;
namespace Demo_Asp_DotNet
{
public partial class cRUD_ASP : System.Web.UI.Page
{
Database1Entities OB = new Database1Entities();
protected void Page_Load(object sender, EventArgs e)
{
}
public void clear()
{
txtaddress.Text = "";
txtage.Text = "";
txtname.Text = "";
txtid.Text = "";
}
protected void btnsave_Click(object sender, EventArgs e)
{
if (btnsave.Text == "Save")
{
Tbl_Student ON = new Tbl_Student();
ON.Student_Name = txtname.Text;
ON.Student_Age = Convert.ToInt32(txtage.Text);
ON.Student_Address = txtaddress.Text;
OB.Tbl_Student.Add(ON);
OB.SaveChanges();
Response.Write("<script>alert('Successfully Saved...')</script>");
clear();
}
else if(btnsave.Text=="Update")
{
int ID = Convert.ToInt32(txtid.Text);
Tbl_Student ON = OB.Tbl_Student.Where(b=>b.Student_Id==ID).FirstOrDefault();
ON.Student_Name = txtname.Text;
ON.Student_Age = Convert.ToInt32(txtage.Text);
ON.Student_Address = txtaddress.Text;
OB.SaveChanges();
Response.Write("<script>alert('Successfully Updated...')</script>");
clear();
btnsave.Text = "Save";
btnreset.Text = "Reset";
}
}
protected void btnreset_Click(object sender, EventArgs e)
{
if (btnreset.Text == "Reset")
{
clear();
}
else if(btnreset.Text=="Delete")
{
int ID = Convert.ToInt32(txtid.Text);
Tbl_Student ON = OB.Tbl_Student.Where(b => b.Student_Id == ID).FirstOrDefault();
OB.Tbl_Student.Remove(ON);
OB.SaveChanges();
Response.Write("<script>alert('Successfully Deleted...')</script>");
clear();
}
}
protected void txtid_TextChanged(object sender, EventArgs e)
{
int ID=Convert.ToInt32(txtid.Text);
Tbl_Student ON = OB.Tbl_Student.Where(b => b.Student_Id == ID).FirstOrDefault();
txtname.Text = ON.Student_Name;
txtage.Text = ON.Student_Age.ToString();
txtaddress.Text = ON.Student_Address;
btnsave.Text = "Update";
btnreset.Text = "Delete";
}
}
}
0 Comments