Archana
Archana

Reputation: 13

Insert data from one table to another using linQ

I have two tables Cart(ItemID, Name, Price, Quantity, CartID, CustID) and PP( invoiceID, itemID, orderQuantity, PricePerUnit, includesID).

Now I have to insert values (ItemID, Price, Quantity) from the Cart table into the pp table where cart.custid = session[customerID]. But the values not getting inserted into the pp table. Not sure if the AsEnumerable syntax used here is correct.

DataClassesDataContext context = new DataClassesDataContext();

var Carts = context.Carts;
pp newpp = new pp();

var p = Carts.AsEnumerable().Select(x => new pp()
{
    itemID = x.ItemID,
    PricePerUnit = Convert.ToDouble(x.Price),
    orderQuantity = x.Quantity,
    invoiceID = invoiceNum

});

context.pps.InsertAllOnSubmit(p);

Upvotes: 1

Views: 1775

Answers (1)

flipchart
flipchart

Reputation: 6578

You don't seem to be calling the SubmitChanges method on the context. This is required to actually persist the data back to the database. The InserAllOnSubmit method merely marks them as needing to be inserted. See here for info

Upvotes: 1

Related Questions