Reputation: 2509
i am developing a project (using 3-tier approach) in which i am using LINQ TO SQL... i want to update user...
but i am facing some problem. it does not give me any error but also do not update the user detail
here is the program sequence;
in UpdateProfile.aspx
String currentUser = Session["BMUser"].ToString();
String displayName = txtDisplayName.Text;
String username = currentUser;
String emailAddress = txtEmailAddress.Text;
String secretQuestion = txtSecretQuestion.Text;
String secretAnswer = txtSecretAnswer.Text;
if (UserManager.UpdateProfile(username, displayName, emailAddress, secretQuestion, secretAnswer))
{
lblStatus.Text = "Profile Updated";
}
else
lblStatus.Text = "Unable to Update Profile";
the UserManager is BLL class
public class UserManager
{
public static bool UpdateProfile(String username, String displayName, String emailAddress, String secretQuestion, String secretAnswer)
{
// This method will return BM_User (BM_User in entity class generated by LINQ TO SQL)
BM_User user = UserCatalog.GetUserByName(username);
if (user != null)
{
user.DisplayName = displayName;
user.EmailAddress = emailAddress;
user.SecretQuestion = secretQuestion;
user.SecretAnswer = secretAnswer;
if (UserManagerDAO.UpdateUser(user, false))
{
//HttpContext.Current.Session["BMUser"] = userToUpdate;
return true;
}
else
return false;
}
else
return false;
}
}
and finally UserManagerDAO
public class UserManagerDAO
{
public static bool UpdateUser(BM_User user, bool changeLoginDateTime)
{
BugManDataContext db = new BugManDataContext();
if (changeLoginDateTime == true)
user.LastLoginDate = DateTime.Now;
db.SubmitChanges();
return true;
}
}
after this when i get the detail of this updated user. it shows me previous detail. mean it is not updating the user's detail with new one...
kindly solve this problem
Upvotes: 2
Views: 1995
Reputation: 41588
The problem is that the user in UserManagerDAO.UpdateUser came from a different data context. To do this, you'll have to use LINQ's serialization if you want to cross over context boundaries.
Here's a couple ways you could get around this.
Both of those options are workarounds - the best option in my opinion would be to use the linq serialization, but if that's not an option, try one of these.
// example code for #2 above...
public class UserManagerDAO
{
public static bool UpdateUser(BM_User user, bool changeLoginDateTime)
{
using(BugManDataContext db = new BugManDataContext())
{
// lookup the current user in the database.
var dbUser = (from u in db.Users
where u.Id == user.Id
select u).Single();
if (changeLoginDateTime == true)
{
// update all the fields from your passed in user object
dbUser.Field1 = user.Field1;
dbUser.Field2 = user.Field2;
dbUser.Field3 = user.Field3;
dbUser.LastLoginDate = DateTime.Now
dbUser.LastLoginDate = DateTime.Now;
db.SubmitChanges();
return true;
}
}
}
}
See the answer on this question for more infomation.
Upvotes: 1
Reputation: 828050
In your UpdateUser method you are declaring a new DataContext, so the BM_User paramter is not attached to it and that's why the SubmitChanges method does nothing.
Upvotes: 2