LINQ: SubmitChanges() not updating my record

2.7k views Asked by At

Not to sound like a broken record here (there a few posts that look like this one) but none of them seem to solve my problem. It seems that when you want to update

private bool resetPassword(string password)
{
    try
    {
       var db = new SchedulerDBDataContext();

       // since this is a instance method, I grab the ID from _this_
       AdminUser user = db.AdminUsers.SingleOrDefault(t => t.ID == _ID);

       if (user != null)
       {
           // this method DOES update these two fields.
           SchedUtil.md5Hash(password, ref user._EncryptedPassword, ref user._PasswordSalt);

           // I threw these in there to try something... it didn't work.
           //user._EncryptedPassword = user.EncryptedPassword;
           //user._PasswordSalt = user.PasswordSalt;

           // this DOESN'T do anything.
           db.SubmitChanges();
           return true;
       }

       return false;
    }
    catch (Exception)
    {
        return false;
    }
}

Maybe this a dumb question but I'm retrieving this from the db... why not just update this's properties. I'm guess I need to pull it through the DBContext I guess.

4

There are 4 answers

1
sgmoore On BEST ANSWER

You should be setting the public properties and not the private values.

  // I threw these in there to try something... it didn't work.
       //user._EncryptedPassword = user.EncryptedPassword;
       //user._PasswordSalt = user.PasswordSalt;

This won't trigger any updates.

Even if you do :

      user.EncryptedPassword = user._EncryptedPassword;
      user.PasswordSalt      = user._PasswordSalt;

this won't trigger any change either as you are not actually changing the values

You can do something like

 string newEncryptedPassword;
 string newPasswordSalt;

 SchedUtil.md5Hash(password, ref newEncryptedPassword, ref newPasswordSalt);

 user.EncryptedPassword = newEncryptedPassword;
 user.PasswordSalt      = newPasswordSalt;

Also check that your table has a primary key, otherwise Linq will not track the changes.

2
Zeus On

DJ,

Are you sure

user._EncryptedPassword , 
user._PasswordSalt 

are the properties ? I think you LINQ TO SQL creates public and private properties.

Can you set them

user.EncryptedPassword , 
user.PasswordSalt

like this ?

Ved

1
p.campbell On

To troubleshoot your code, try any of these suggestions:

  • while debugging the code, I'll assume that your object is not null.
  • ensure that your properties are actually changed. It's odd that you're using pipe-prefixed field names, but either way: while debugging, check that your properties actually have new values.
  • use SQL Server Profiler to capture the SQL statement sent to the database server. You'll then be able to re-run this UPDATE query back into SQL Management Studio, and determine how many records are effected. You'll also be able to see the values passed in the UPDATE statement.
0
Jeff Mercado On

Ved pointed out one possible problem. Just in case that doesn't work, you should double check your LINQ to SQL class' AdminUser class definition and make sure that the generated code implements the INotifyPropertyChanging and INotifyPropertyChanged interfaces. There are some cases where the designer will not implement these interfaces which prevents updates from working. e.g., not declaring a primary key.