简体   繁体   中英

issue in update record in wpf using Linq to sql concept

        MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
        var _Update = from u in MyDB.Employees where u.Address == "WestSreet" select u;
        foreach (var item in _Update)
        {
            item.Address = "WS";
        }
        MyDB.SubmitChanges();
        var Select = from s in MyDB.Employees select s;
        grd_1.ItemsSource = Select;

With above code i can update the record and i able see the record modification in Original table data and DataGrid as well.The original table is updated successfully.

        MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
        var _Udate_2 = MyDB.Employees.Where(u => (u.Address == "WestSreet"));
        foreach (var item in _Udate_2)
        {
            item.Address = "WS";
        }
        var Select = from s in MyDB.Employees select s;

        grd_1.ItemsSource = Select;

With above code i can update the record.I can see modification in DataGrid But i can't see the record modification in Original table data.The original table is not updated.

You are missing a

  MyDB.SubmitChanges();

On the second bit of code.

It should be this:

    MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
     var _Udate_2 = MyDB.Employees.Where(u => (u.Address == "WestSreet"));
    foreach (var item in _Udate_2)
    {
        item.Address = "WS";
    }
    MyDB.SubmitChanges();
    var Select = from s in MyDB.Employees select s;

    grd_1.ItemsSource = Select;

Because in 2nd code you are not calling MyDB.SubmitChanges();

MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
        var _Udate_2 = MyDB.Employees.Where(u => (u.Address == "WestSreet"));
        foreach (var item in _Udate_2)
        {
            item.Address = "WS";
        }
MyDB.SubmitChanges(); // Need to call that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM