Unable to update DB using Linq To Entities

Posted by Sharpcnet under C# on 12/15/2013 | Points: 10 | Views : 925 | Status : [Member] | Replies : 2
I am trying to insert a new record into tblUser and also update a record in tblEmployee at the same time. The new record in tblUser saves fine, but the update part of second table is not working. The complier reads tbEmp.IsUser = true; this line though. What am I missing.
public void Save(tblUser objUser)
{
MYDB.AddTotblUsers(objUser);

tblEmployee tbEmp = this.GetEmployeeRecordById(objUser.EmpId);
if(tbEmp!=null)
{
string abc = tbEmp.EmployeeCode;
tbEmp.IsUser = true;
}
MYDB.SaveChanges();
}

public tblEmployee GetEmployeeRecordById(Guid gId)
{
return MYDB.tblEmployees.Where(e=>e.EmployeeId == gId).SingleOrDefault();
}
The EmpId is known already and is not necessary that tblUser has to Save First before updating tblEmployee . To make sure, I checked 'abc', I get that employees code there...




Responses

Posted by: vishalneeraj-24503 on: 12/15/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer:-
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
http://www.codeproject.com/Articles/37938/Simple-6-steps-to-use-stored-procedure-in-LINQ
http://msdn.microsoft.com/en-us/library/bb386946%28v=vs.110%29.aspx

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

Posted by: Bandi on: 12/16/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
apply try..catch for save functionality and also check the tbEmp value by debug Save() method
try  
{
// save changes to the database
MYDB.SaveChanges();
}
catch
{
throw new Exception("Could not save changes.");
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Login to post response