Please i need a Solution to Linq Update

Posted by Olubjay under C# on 5/21/2013 | Points: 10 | Views : 576 | Status : [Member] | Replies : 1
i av tried this but the result is not reflecting on my database:


int ge = context.GeneralStockTBs.Where(aa => aa.Goods_Type_ID.Equals(ide)).Select(a => a.Total_each_quantity).Single();
int compute = ge - quanty;
stockk.General_Stock_ID = stockk.General_Stock_ID;
stockk.Goods_Type_ID = stockk.Goods_Type_ID;
stockk.GoodsnameGeneral = stockk.GoodsnameGeneral;
stockk.st_id = stockk.st_id;
stockk.Stocking_Date = stockk.Stocking_Date;
stockk.Total_amount_quantity = compute;
//stockk.Total_each_quantity = stockk.Total_each_quantity;
try
{
context.SubmitChanges();
MessageBox.Show("Update");
}
catch
{
MessageBox.Show("cannot update");
}

Olubjay


Responses

Posted by: Rabbil on: 5/22/2013 [Member] Starter | Points: 25

Up
0
Down
Hi!
For updating the DB using LINQ You have to use Attach() and after that Applycurrent() changes...
Check the below code.

var update = (from x in context.EntityName
where x.Goods_Type_ID== objToMatchID.Goods_Type_ID ///here on which basis u want to update match your criteria on where clause,
select x).FirstOrDefault();
EntityName.Attach(objToMatchID); ///By using Attach() DataContext knows the entity is meant to be updated and should not be inserted into the //database.
try
{
context.EntityName.ApplyCurrentValues(objToMatchID); //Copies the scalar values from the supplied object into the object in the DataContext that has //the same key
context.SaveChanges();
MessageBox.Show("Update");
}
catch
{
MessageBox.Show("cannot update");
}


Hope this would help

"I cannot teach anybody anything. I can only make them think"~ Socrates
Thanks And Regards

Olubjay, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response