Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 2 - Insert Update Delete using LINQ to SQL

Suggested Videos
Part 1 - LINQ to SQL



In this video, we will discuss performing Insert Update and Delete using LINQ to SQL. We will continue with the example, we worked with in Part 1. In Part 1, we discussed performing a Select using LINQ to SQL. Please watch Part 1 before proceeding.



Insert using LINQ to SQL
using (SampleDataContext dbContext = new SampleDataContext())
{
    Employee newEmployee = new Employee
    {
        FirstName = "Tim",
        LastName = "T",
        Gender = "Male",
        Salary = 55000,
        DepartmentId = 1
    };

    dbContext.Employees.InsertOnSubmit(newEmployee);
    dbContext.SubmitChanges();
}

Update using LINQ to SQL
using (SampleDataContext dbContext = new SampleDataContext())
{
    Employee employee = dbContext.Employees.SingleOrDefault(x => x.ID == 8);
    employee.Salary = 65000;
    dbContext.SubmitChanges();
}

Delete using LINQ to SQL
using (SampleDataContext dbContext = new SampleDataContext())
{
    Employee employee = dbContext.Employees.SingleOrDefault(x => x.ID == 8);
    dbContext.Employees.DeleteOnSubmit(employee);
    dbContext.SubmitChanges();
}

Now let us continue with the example that we worked with in Part 1.

Step 1 : Drag and drop 4 button controls on the webform. Change the Text & ID properties of the 4 button controls as shown below.
Button 1 : Text = Get Data, ID = btnGetData
Button 2 : Text = Insert, ID = btnInsert
Button 3 : Text = Update, ID = btnUpdate
Button 4 : Text = Delete, ID = btnDelete

Double click on each of the button controls to generate their respective click event handler methods.

At this point, the design of the WebForm should be as shown below.
insert update delete using linq to sql

Step 2 : Copy and paste the following code in the code-behind file.
using System;
using System.Linq;

namespace Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {}

        private void GetData()
        {
            SampleDataContext dbContext = new SampleDataContext();
            GridView1.DataSource = dbContext.Employees;
            GridView1.DataBind();
        }

        protected void btnGetData_Click(object sender, EventArgs e)
        {
            GetData();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            using (SampleDataContext dbContext = new SampleDataContext())
            {
                Employee newEmployee = new Employee
                {
                    FirstName = "Tim",
                    LastName = "T",
                    Gender = "Male",
                    Salary = 55000,
                    DepartmentId = 1
                };

                dbContext.Employees.InsertOnSubmit(newEmployee);
                dbContext.SubmitChanges();
            }

            GetData();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            using (SampleDataContext dbContext = new SampleDataContext())
            {
                Employee employee = dbContext.Employees.SingleOrDefault(x => x.ID == 8);
                employee.Salary = 65000;
                dbContext.SubmitChanges();
            }

            GetData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            using (SampleDataContext dbContext = new SampleDataContext())
            {
                Employee employee = dbContext.Employees.SingleOrDefault(x => x.ID == 8);
                dbContext.Employees.DeleteOnSubmit(employee);
                dbContext.SubmitChanges();
            }

            GetData();
        }
    }
}

LINQ to SQL Tutorial

3 comments:

  1. Hello Sir,
    Your videos are giving me the best my knowledge.Please, make some videos on
    Design Patterns in .Net.
    Thanks a ton Sir.

    ReplyDelete
  2. Your viedos are really very helpful. Can you please make some viedos on windows Azure

    ReplyDelete
  3. please can you explain iqueryable vs ienumerable c#

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.