none
LINQ to SQL is not updating database RRS feed

  • Question

  • I have a problem with LINQ to SQL because the code is not updating database. Please help

    DataClasses1DataContext db = new DataClasses1DataContext();
    
                var result = from x in db.USER_LINQTOSQLs
                                      where username == x.Username
                                      select x;
    
               USER_LINQTOSQL user = result.First();

      user.ModifiedBy = currentUser.Username; user.ModifiedDt = DateTime.Now; user.Status = status; db.SubmitChanges();
    Monday, September 21, 2009 8:48 AM

Answers

  • You are creating a new object of type USER_LINQTOSQL that you assign your result from result.First(), the problem is that your new object user is not part of the datacontext and so your SubmitChanges() has no effect. You need to work on the result object or add your user object to the data context ( which is not what ou want here since you are only changing a record and not wanting to insert a new one ).

    Formerly MichaelTx
    • Marked as answer by Yichun_Feng Monday, September 28, 2009 1:15 AM
    Monday, September 21, 2009 1:53 PM

All replies

  • For some strange reason the code worked. Im not sure what happened yesterday when code produced nothing.
    Monday, September 21, 2009 10:03 AM
  • I have worked only part of the problem. That code worked because I have put the query into that function from other function in other class.

    In that function in other class i have had another datacontext class which was not disposed. I have disposed it but the problem still persist.

    Here is the code:

    public class UserCommand {
    
    ...
    
    public static USER_LINQTOSQL FindUser(string username)
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    var result = from x in db.USER_LINQTOSQLs
                                 where username == x.Username
                                 select x;
    
                    return result.First();
                }
            }
    
    ...
    
    }
    
    public class UserManager {
    
    ... 
    
    public void Unlock(string username)
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    USER_LINQTOSQL user = UserCommand.FindUser(username);
    
                    user.ModifiedBy = currentUser.Username;
                    user.ModifiedDt = DateTime.Now;
                    user.Status = "STATUS_ENABLED";
                    user.LogOnFailedCnt = 0;
    
                    db.SubmitChanges();
                }
            }
    
    ...
    
    }

    Monday, September 21, 2009 11:26 AM
  • You are creating a new object of type USER_LINQTOSQL that you assign your result from result.First(), the problem is that your new object user is not part of the datacontext and so your SubmitChanges() has no effect. You need to work on the result object or add your user object to the data context ( which is not what ou want here since you are only changing a record and not wanting to insert a new one ).

    Formerly MichaelTx
    • Marked as answer by Yichun_Feng Monday, September 28, 2009 1:15 AM
    Monday, September 21, 2009 1:53 PM