zubi
zubi

Reputation: 11

Ways to update only modified columns in table using LINQ

I have 20 fields on form, how to update fields modified by user during runtime and how to check which fields have changed so that i can only update those values in table using LINQ. I am working on windows application using C# and VS2010

Please refer the code (Currently i am passing all values, i knw this is not the correct way)

    private void UpdateRecord(string groupBoxname)
        {
            using (SNTdbEntities1 context = new SNTdbEntities1())
            {
                {
                    Vendor_Account va = new Vendor_Account();                    

                    var Result = from grd in context.Vendor_Account
                                 where grd.Bill_No == dd_billNo.Text
                                 select grd;

                    if (Result.Count() > 0)

                        if ((dd_EditProjectName.Text!= "Select") && (dd_billNo.Text!="Select") && (dd_editVendorName.Text!="Select"))
                        {
                            foreach (var item in Result)
                            {
                                va.Account_ID = item.Account_ID;
                            }
                            va.Amount_After_Retention = Convert.ToDecimal(txt_AD_AfterRet.Text);
                            va.Balance = Convert.ToDecimal(txt_AD_Balance.Text);
                            va.Bill_Amount = Convert.ToDecimal(txt_AD_BillAmount.Text);
                            va.Bill_Date = Convert.ToDateTime(dt_AD_BillDate.Text);
                            va.Bill_No = dd_billNo.Text;
                            va.Comments = txt_AD_Comments.Text;
                            va.Paid_Till_Date = string.IsNullOrEmpty(txt_AD_Paid.Text)?0:Convert.ToDecimal(txt_AD_Paid.Text);
                            va.Project_Name = dd_EditProjectName.Text;
                            va.Retention_Perc = Convert.ToDecimal(txt_retPerc.Text);
                            va.Amount_After_Retention = Convert.ToDecimal(txt_AD_AfterRet.Text);
                            va.Vendor_Name = dd_editVendorName.Text;
                            va.Vendor_Code = txt_AD_Code.Text;

                            context.Vendor_Account.ApplyCurrentValues(va);
                            //context.Vendor_PersonalInfo.AddObject(vpi);
                            context.SaveChanges();
                            MessageBox.Show("Information Updated Sucessfully!");
                            lbl_Warning.Text = "";
                            entityDataSource1.Refresh();
                        }

                        else
                        {
                            MessageBox.Show("Vendor Name,Project Name and Bill No cannot be blank!!");
                        }               

                }               

            }
        }

Upvotes: 1

Views: 2230

Answers (2)

Boomer
Boomer

Reputation: 1478

Entity framework will do that task. Since you didn't provide any code, I cannot be precise about the answer but please check those links:

http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx, section:Manipulating Data and Persisting Changes

http://www.codeproject.com/KB/database/sample_entity_framework.aspx

Note that the SaveChanges() function will update any modification done the records in EF.

Upvotes: 2

Alexander Molodih
Alexander Molodih

Reputation: 1936

Create some field dublicates, and compare value from the form element with the local dublicate, if it was changed than update it.

Upvotes: -1

Related Questions