How to update record using LINQ programmtically?

SheoNarayan
Posted by SheoNarayan under LINQ category on | Views : 29867
Write following code.

DataUsersClassesDataContext dataUsers = new DataUsersClassesDataContext(); // LINQ object
// Users is the database table inside the LINQ object, AutoID is the primary key that is used to compare the value
ThisUser user = dataUsers.Users.Single(u => u.AutoID == XXXX);
if (user != null)
{
// set properties
user.Active = active;
user.Name = "MyName";
user.Role = role;
user.ThisDateTime = DateTime.Parse(thisDateTime);
dataUsers.SubmitChanges(); // submit the changes that will update the database.
}

Comments or Responses

Posted by: vinodkus-5813 on: 12/15/2010 Level:Starter | Status: [Member] | Points: 10
I am using same thing. it is not giving any error but it does not reflect in database.
Posted by: vinodkus-5813 on: 12/15/2010 Level:Starter | Status: [Member] | Points: 10
// I am doing like this, but not changes in database. Checked through debugging. It is going right.

LinqClassesForCategoryDataContext db = new LinqClassesForCategoryDataContext();

var record = db.Categories.Single(t => t.CategoryId == 1);
if (record != null)
{
record.Description = "Vinod";
db.SubmitChanges();
}

Login to post response