How To Update Data Using LINQ To SQL [ C#, ASP.NET Programming]

ASPX Page Icon I’m working with LINQ for the past few weeks. It is a very beautiful technology but with very limited documentation. Today I tried updating database tables with the data captured in a web form.

The code we initially used to update the database tables with the help of LINQ to SQL  is as follows

//Initiate LINQ objects
pobjConsultant = new t_consultant();
pobjAddress = new t_address();
pobjContactDetails = new t_contact();
pobjBankRecord = new t_bank_record();
//
//Gather data from form fields and assign values to the objects
//....
//....code for assigning values...
//....

using (XITTimesheetDataContext db = new XITTimesheetDataContext())
{
    //
    db.t_consultants.Attach(pobjConsultant);
    // our design does not have a FK between Consultant and address
    // as address table is used hold data from many other tables
    // like Consultant.
    db.t_addresses.Attach(pobjAddress);
    db.t_bank_records.Attach(pobjBankRecord);
    db.t_contacts.Attach(pobjContactDetails);
    db.SubmitChanges();
}

The above code executes without any error but it does not update values in the database tables. After execution of the code none of the new values are updated to the database tables.

Googling through forums revealed that we need to change the code as the following

//Initiate LINQ objects
pobjConsultant = new t_consultant();
pobjAddress = new t_address();
pobjContactDetails = new t_contact();
pobjBankRecord = new t_bank_record();
//
//Gather data from form fields and assign values to the objects
//....
//....code for assigning values...
//....
using (XITTimesheetDataContext db = new XITTimesheetDataContext())
{
    //
    db.t_consultants.Attach(pobjConsultant);
    db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, pobjConsultant);
    // our design does not have a FK between Consultant and address
    // as address table is used hold data from many other tables
    // like Consultant.
    db.t_addresses.Attach(pobjAddress);
    db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, pobjAddress);
    //
    db.t_bank_records.Attach(pobjBankRecord);
    db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, pobjBankRecord);
    //
    db.t_contacts.Attach(pobjContactDetails);
    db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, pobjContactDetails);
    //
    db.SubmitChanges();
}

After adding db.Refresh() method calls before submitting the changes solved the problem and the values are updated in the database tables properly.

Not sure about the drawbacks and implication of calling Refresh() method with KeepCurrentValues parameter. But solved my problem of the day. If you are aware of any implications please let me know.

2 thoughts on “How To Update Data Using LINQ To SQL [ C#, ASP.NET Programming]”

  1. Actually you can do away with the ‘Attach’ method and instead use the following construct.

    using (XITTimesheetDataContext db = new XITTimesheetDataContext())
    {

    pobjConsultant = new t_consultant();

    //Gather data from form fields and assign values to the objects
    //....
    //....code for assigning values...
    //....

    db.t_consultant.InsertOnSubmit();
    db.SubmitChanges();
    }

Leave a Comment

Your email address will not be published. Required fields are marked *