views:

476

answers:

3

Hi

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

+2  A: 

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.

CMS
hmm. ok how to solve this problem.... ???? if i do db.BM_User.Attach(user) it gives me error. that can not attach this entity. it is declared in some where else. this is not possible.
Mohsan
I think you should look again your class design, and maybe handle a DataContext in your "UserManagerDAO" class, also GetUserByName(username) shouldn't be on your DAO class also??
CMS
GetUserByName(username) method is part of UserCatalog which actually calls the GetUserByName(username) method of UserManagerDAO class...
Mohsan
+1  A: 

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.

  1. Add a ROWVERSION column to your table, and persisting that with your object. Once you've done that, and you attach the object back to your data context, the update should work as expected.
  2. re-select the record in your update method, and manually copy the properties from your updated object to the selected object. You could also use reflection for this if you wanted to make it work for all of your tables instead of just this one. Sample code for this is below.

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.

Scott Ivey
Or you can pass the same datacontext around so everyone is using the same one.
Frank Schwieterman
any other solution rather that timestamp. i dont want to change all tables in database.
Mohsan
please someone solve this problem :(((((
Mohsan
A: 

i added time stamp column. but still it does not updating the detail of user

Mohsan
please someone solve this problem :(((((
Mohsan
are you attaching your user back to the context before submitting changes with the timestamp? and are you making sure to persist the timestamp?
Scott Ivey