Basic CRUD operation using Entity Framework and ADO.NET

Hi.This is my first post at MyDotProgramming. Because i have learnt a lot from internet during my job so i want to give back to internet community. There are so many articles written on the same topic but here,my motto is to help starter. If you are trying to learn Entity Framework first time then i suggest you to read this article thoroughly.If you want just more than CRUD operation in entity framework then you should wait for my next article in this series.

I assume that you already know what is Entity Framework and What is ADO.NET. I will show you how to perform basic database operation here in EF.

Follow these steps to see a live action:

1. In VS 2010/2012, add a new blank Web Application Project.

entity framework ado net

2. Open SQL server management studio 2008/2012 and add a new database “Tutorial”.Now compile below script to add a simple registration table.

USE [Tutorial]
GO

/****** Object: Table [dbo].[registration] Script Date: 07/25/2014 23:43:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[registration](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
[email] [nvarchar](50) NULL,
CONSTRAINT [PK_registration] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

3. Come back to VS, right click on project and click on add a new item.Select Data as installed templates. After that,find ADO.NET Entity Data Model item in list. Rename item name to Tutorial.Finally click on add button.

entity framework adonet crud

4. In the next step, you have to choose model contents.Select generate from database option and follow the simple wizard steps.

entity framework crud operation using ado.net

5. Next step will ask you to add connection to recently added database. Also there is optional setting to save connection string to Web.config file.

6. Finally you need to add database objects to your Entity Model and finish the wizard step.

7. You can see that a new file named “Tutorial.edmx” is available in our project.

8. Add a webform page to project to design a basic registration form. See below screenshot to verify the same.

entity framework crud web form

9. Checkout c-sharp code written for CRUD operation in EF using ADO.NET.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyDotProgramming
{
public partial class WebForm1 : System.Web.UI.Page
{
TutorialEntities tt = new TutorialEntities();
registration rg = new registration();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgrid();
}
}

void fillgrid()
{
GridView1.DataSource = tt.registrations;
GridView1.DataBind();

}
protected void Button1_Click(object sender, EventArgs e)
{
rg.name = TextBox1.Text;
rg.email = TextBox2.Text;

tt.registrations.AddObject(rg);
tt.SaveChanges();
TextBox1.Text = “”;
TextBox2.Text = “”;
fillgrid();
}

protected void Button2_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox3.Text);
registration update = (from r in tt.registrations.Where(a => a.id == id) select r).FirstOrDefault();
update.name = TextBox4.Text;
tt.SaveChanges();
fillgrid();
TextBox3.Text = “”;
TextBox4.Text = “”;

}

protected void Button3_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox3.Text);
registration delete = (from r in tt.registrations.Where(a => a.id == id) select r).FirstOrDefault();
tt.registrations.DeleteObject(delete);
tt.SaveChanges();
fillgrid();
TextBox3.Text = “”;
TextBox4.Text = “”;
}

}

}

10. Make sure that all required references and name spaces are added.

Download : Tutorial.zip

Madan Gehlot

I am a Dot Net Developer. I like to share my knowledge. I love blogging.

2 thoughts to “Basic CRUD operation using Entity Framework and ADO.NET”

  1. I will like to co-admin with you for your blog – mydotprogramming
    I am also DotNet Developer in practising phase.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.