Entity Framework CRUD- Select Insert Update Edit Delete using Entity Framework in ASP.Net

Entity Framework CRUD- Select Insert Update Edit Delete using Entity Framework in ASP.Net

In this article, you will learn how to perform select, insert, edit, update, delete using Entity Framework in ASP.Net using C#.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerId"
OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
<Columns>
    <asp:TemplateField HeaderText="Name" ItemStyle-Width="140">
        <ItemTemplate>
            <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Country" ItemStyle-Width="140">
        <ItemTemplate>
            <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="140"/>
</Columns>
</asp:GridView>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tr>
    <td style="width: 140px">
        Name:<br />
        <asp:TextBox ID="txtName" runat="server" Width="150" />
    </td>
    <td style="width: 140px">
        Country:<br />
        <asp:TextBox ID="txtCountry" runat="server" Width="150" />
    </td>
    <td style="width: 90px">
        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
    </td>
</tr>
</table>
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BindGrid();
    }
}
 
private void BindGrid()
{
    using (CustomersEntities entities = new CustomersEntities())
    {
        GridView1.DataSource = from customer in entities.Customers select customer;
        GridView1.DataBind();
    }
}
protected void Insert(object sender, EventArgs e)
{
    using (CustomersEntities entities = new CustomersEntities())
    {
        Customer customer = new Customer
        {
            Name = txtName.Text,
            Country = txtCountry.Text
        };
        entities.AddToCustomers(customer);
        entities.SaveChanges();
    }
 
    this.BindGrid();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    this.BindGrid();
}
protected void OnRowCancelingEdit(object sender, EventArgs e)
{
    GridView1.EditIndex = -1;
    this.BindGrid();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.RowIndex];
    int customerId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    string name = (row.FindControl("txtName") as TextBox).Text;
    string country = (row.FindControl("txtCountry") as TextBox).Text;
    using (CustomersEntities entities = new CustomersEntities())
    {
        Customer customer = (from c in entities.Customers
                            where c.CustomerId == customerId
                            select c).FirstOrDefault();
        customer.Name = name;
        customer.Country = country;
        entities.SaveChanges();
    }
    GridView1.EditIndex = -1;
    this.BindGrid();
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int customerId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    using (CustomersEntities entities = new CustomersEntities())
    {
        Customer customer = (from c in entities.Customers where c.CustomerId == customerId
                                select c).FirstOrDefault();
        entities.DeleteObject(customer);
        entities.SaveChanges();
    }
    this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
    {
        (e.Row.Cells[2].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
    }
}

 

2021-10-03