Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Entity Framework CRUD Operations with GridView in ASP.NET

In this tutorial, I am going to explain you CRUD (Create, Read, Update and Delete) operations using Entity Framework with GridView control in ASP.NET. I will be using Visual Studio 2013, SQL-Server 2008R2 and Entity Framework 6.0. Below is the step by step tutorial.

Creating Database Table

We will be using two tables tbl_UserMaster and tbl_Country. tbl_UserMaster table is used to store User Details and tbl_Country table is used to bind country name in DropDownList while adding new record and editing existing record.

UserMaster and Country Table

Below is the script to create tables and add dummy data into it.

create tabletbl_UserMaster
UserId int primary key identity,
Name varchar(50),
Email varchar(50),
ContactNo varchar(15),
Gender varchar(10),
Country varchar(50)
insert intotbl_UserMaster values
('Rahul','[email protected]','9123456789','Male','India'),
('John','[email protected]','8123456789','Male','Austraila'),
('Mary','[email protected]','7123456789','Female','USA'),
('Mike','[email protected]','6123456789','Male','UK')
create tabletbl_Country
CountryId int primary key identity,
Name varchar(50)
insert intotbl_Country values

Creating ASP.NET Empty Project

Next step is to create ASP.NET Empty WebForm project.
Go to FileNewProject. A new window will be open as shown below.
Now go to WebVisual Studio 2012 → select .NET Framework 4.5 → select ASP.NET Empty Web Application and give project name and click on OK.

Now, an empty project will be created.

Adding Entity Data Model And Entity Framework

Now, next step is to add entity data model and entity framework.

Go to WebApplication1 root folder → AddNew Item. A new window will be open as shown below.
Go to Data Tab and select ADO.NET Entity Data Model and give its name SampleModel.edmx and click on OK.
Select Generate from database and Click on next
Click on New Connection.
Select your server, write your credentials, select database and test for connection.
Write connection string name.
Select Entity Framework version 6.0 and click on Next.
Click select your table tbl_Country and tbl_UserMaster and click on Finish.

Adding WebForm

Now, add a new webform to the project. Create a GridView Control as shown below.

GridView and CSS Code:
@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

html xmlns="">
head runat="server">
form id="form1" runat="server">
asp:GridView ID="gvUserMaster" runat="server" AutoGenerateColumns="false" PageSize="5" AllowPaging="true"
    DataKeyNames="UserId" ShowFooter="true" CellPadding="3" ForeColor="#333333" OnPageIndexChanging="gvUserMaster_PageIndexChanging"
    OnRowEditing="gvUserMaster_RowEditing" OnRowUpdating="gvUserMaster_RowUpdating" OnRowCancelingEdit="gvUserMaster_RowCancelingEdit"
    OnRowDeleting="gvUserMaster_RowDeleting" AlternatingRowStyle-BackColor="White" GridLines="Horizontal">
        asp:TemplateField HeaderText="User Name" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="150px">
                asp:Label ID="lblName" runat="server" Text='# Bind("Name") %>'>asp:Label>
                asp:TextBox ID="etxtName" runat="server" Text='# Bind("Name") %>'>asp:TextBox>
                asp:RequiredFieldValidator ID="rfvetxtName" runat="server" ControlToValidate="etxtName" ErrorMessage="*"
                    Display="Dynamic" ForeColor="Red" SetFocusOnError="True" ValidationGroup="vgEditRecord">
                asp:TextBox ID="ftxtName" runat="server">asp:TextBox>
                asp:RequiredFieldValidator ID="rfvftxtName" runat="server" ControlToValidate="ftxtName" ErrorMessage="*"
                    Display="Dynamic" ForeColor="Red" SetFocusOnError="True" ValidationGroup="vgAddRecord">
        asp:TemplateField HeaderText="User Email" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="150px">
                asp:Label ID="lblEmail" runat="server" Text='# Bind("Email") %>'>asp:Label>
                asp:TextBox ID="etxtEmail" runat="server" Text='# Bind("Email") %>'>asp:TextBox>

This post first appeared on ASPArticles, please read the originial post: here

Share the post

Entity Framework CRUD Operations with GridView in ASP.NET


Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription