2

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.

aarona
  • 35,986
  • 41
  • 138
  • 186

4 Answers4

4

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.

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • This was my problem! I apparently you can't pass properties into a method that requires them to be `ref` – aarona Feb 07 '11 at 18:05
3

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

Zeus
  • 3,091
  • 6
  • 47
  • 60
  • 1
    Additionally it looks like he is setting his private properties to equal their public counterparts so while it is updating the database it is doing so with the same values so no change – Adrian Feb 05 '11 at 03:13
  • @Aliester +1 and +1 to Ved as well. However I accepted another answer because it actually was dealing with the under lying problem which is that originally I tried to pass the public properties to my method but I got a compiler error because you can't pass properties by reference apparently. – aarona Feb 07 '11 at 18:07
0

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.
p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • @p.cambell re: passing my private fields, I originally passed public properties but was getting compiler error because you can't pass properties to a method that requires them parameters to be `ref` ed. – aarona Feb 07 '11 at 18:10
0

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.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272