Update using LINQ to sql

In this artical we will learn how to update records using linq to sql. We need to pass the class object which we want to update. So we will fetch the records from database using id and then we will add the updated data.




We use the following example we need to update the department. so we will set the change department value to the dept coulmn.

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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //create the object of data context class and pass the connection string
        DataContext dc = new DataContext("Data Source=ISHA-PC;Initial Catalog=TechAltum;Integrated Security=True");

        //create object of class which mapped the table in database
        Class1 cal_data;
       

        //set the where clause for update the data
        cal_data = dc.GetTable<Class1>().Single(res => res.id == 4);

        //add updated data
        cal_data.dept = "changeDept";

       
        dc.GetTable<Class1>();
        dc.SubmitChanges();
      
      
    }
}