Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

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

5 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
  4. I'm getting this error while inserting data from my form to database using LinQ

    DBContext..InsertOnSubmit()
    When I enter the object of the class(Employee) It shows the error.

    Error 1 The best overloaded method match for 'System.Data.Linq.Table.InsertOnSubmit(Linqtosql1.tblArticle)' has some invalid arguments

    Please do reply how to recover it.

    ReplyDelete
  5. Seems very basic. You are using a magic number to edit or delete the record, however, getting ID from gridview is way difficult. Please add some code to show how to retrieve the ID from selected row on gridview control.

    ReplyDelete

It would be great if you can help share these free resources