How to do CRUD Operation using EF 6.0 in asp.net ?


EmpDetails

In EF 6.0,some old method has been repleaced with new one like instead of DeleteObject() replaced with Remove() and so many other new features has been added.

This article is the replacement of My old article.

https://chandradev819.wordpress.com/2011/05/13/how-to-do-insertupdatedelete-using-entity-framework-in-asp-net/

Step1:In aspx page design the form as per as above UI and configure the Gridview like this

 <asp:GridView ID="GridView1" runat="server" EnableViewState="true" AutoGenerateColumns="False" Width="446px" OnRowCommand="GridView1_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="No">
                                    <ItemTemplate>
                                        <%#Container.DataItemIndex+1 %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="EmpName" DataField="EmpName" />
                                <asp:BoundField HeaderText="EmpAddress" DataField="EmpAddress" />
                                <asp:TemplateField HeaderText="Action">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="EditRow"  CommandArgument='<%# Eval("Id") %>' OnClientClick="return confirm('Are you Sure?')"  CausesValidation="false" />
                                        <asp:LinkButton ID="lnlDelete" runat="server" Text="Delete" CommandName="DeleteRow" CommandArgument='<%# Eval("Id") %>' OnClientClick="return confirm('Are you Sure?')" CausesValidation="false" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        <asp:HiddenField ID="HiddenField1" runat="server" />

Step2: Write the code in code behind like this



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

namespace EFTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadEmpData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (btnSubmit.Text == "Submit")
            {
                SaveEmpData();
            }
            else
            {
                EditEmpData();
            }
            LoadEmpData();
        }

        private void SaveEmpData()
        {
            using (EmpDBEntities dbEntities = new EmpDBEntities())
            {
                tblEmp objEmp = new tblEmp();
                objEmp.EmpName = txtEmpName.Text;
                objEmp.EmpAddress = txtEmpAddress.Text;
                dbEntities.tblEmps.Add(objEmp);

                dbEntities.SaveChanges();
                txtEmpName.Text = string.Empty;
                txtEmpAddress.Text = string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void EditEmpData()
        {
            using (EmpDBEntities dbEntities = new EmpDBEntities())
            {
                int id = Convert.ToInt32(HiddenField1.Value);
                tblEmp objEmp = dbEntities.tblEmps.Find(id);
                objEmp.EmpName = txtEmpName.Text;
                objEmp.EmpAddress = txtEmpAddress.Text;
                dbEntities.SaveChanges();

                txtEmpName.Text = string.Empty;
                txtEmpAddress.Text = string.Empty;
                btnSubmit.Text = "Submit";
            }
        }

        private void LoadEmpData()
        {
            using (EmpDBEntities dbEntities = new EmpDBEntities())
            {
                GridView1.DataSource = dbEntities.tblEmps.ToList();
                GridView1.DataBind();
            } 
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "DeleteRow")
            {
                using (var dbEntities = new EmpDBEntities())
                {
                    tblEmp objEmp = dbEntities.tblEmps.Find(id);
                    dbEntities.tblEmps.Remove(objEmp);
                    dbEntities.SaveChanges();
                    LoadEmpData();
                }
            }
            else if (e.CommandName == "EditRow")
            {
                using (EmpDBEntities dbEntities = new EmpDBEntities())
                {
                    tblEmp objEmp = dbEntities.tblEmps.Find(id);
                    txtEmpName.Text = objEmp.EmpName;
                    txtEmpAddress.Text = objEmp.EmpAddress;
                    HiddenField1.Value = objEmp.Id.ToString();
                    btnSubmit.Text = "Update";
                }
            }
        }
    }
}


Advertisements
Posted in EF 6.0. 1 Comment »

One Response to “How to do CRUD Operation using EF 6.0 in asp.net ?”

  1. amit kumar Says:

    whya are using the using EFTest.Models; namespace


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: