0

I have this function:

//change user password
void chgPass(string username, string old_password, string new_password)
{
    var result = (from l in this.dbLogin.SLN
                 where l.user_id.Equals(username)
                 where l.password.Equals(old_password)
                 select l).First();

    result.password= new_password;

    dbLogin.SubmitChanges();
}

I call it like this:

this.loginClient.chgPassAsync(user, old_password, new_password);

Can someone tell me why no changes are maded when I run this... in debuge mode I can see that password field is changed, but when I look in db password isn't changed.

Wolfy
  • 4,213
  • 8
  • 35
  • 42

5 Answers5

2

Does your SLN table has a primary key? if not then try to set one, according to my test LINQ wont update an entity without having PK in the table it belongs to
please check this too

Community
  • 1
  • 1
Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
1

Wolfy - I would first fire up SQL Profiler to determine if a SQL Update statement is being sent to your target SQL Server. If it is, I would then check to make sure you won't have a transaction active, at a higher level, that isn't being committed or is being rolled back. Finally, if the first two don't solve the problem, make sure there is an actual change in your entity.

I'm assuming the First() method isn't throwing an exception, which it will do if no rows are found. So your query must at least be returning a row of data.

Be careful with this call:

this.loginClient.chgPassAsync(user, old, new);

'new' is a C# keyword.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
1

I had answered a question like this before and I look for that but I can't find it, so I repeat my answer here again,

If you've added the Database file to your solution every time that you run your application a copy of it goes to your Debug folder, and you change that file not the original one,

Ans another chance is that you password data type is Binary and update will fail because of columns data type and problems that exist in updating binary data

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
0

Where does star_password in where l.password.Equals(star_password) come from?

Should it not be old_password?

Also new is a reserved word in c# so you should use something else in this.loginClient.chgPassAsync(user, old, new);

Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85
0

Your method looks weird. What are usernamene and star_password ?

It should be more like this:

    void chgPass(string username, string old_password, string new_password) 
    { 
        var result = this.dbLogin.SLN.FirstOrDefault(l => l.user_id == username 
                          && l.password == old_password);
        if(result == null) 
            throw new ArgumentException("User not found", "username");

        result.password = new_password; 
        this.dbLogin.SubmitChanges(); 
    } 
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222