title: C# Entity Framework Entity State Modified
body: <p>I am pretty new to C#. I am trying to make an user profile database and display it in a WPF listview in C#, with Entity Framework code-first.</p>
  
  <p>I have managed to add and delete profiles to the database, and display them.
  I got stuck on modifying an existing object and after a while of trying to make it work, the closest thing I got was that the selected object (selected in the listview) makes the one above it receive the update. </p>
  
  <p>Here is the code for the "Save" button</p>
  
  <pre><code>private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
  {
          using (UsersContext ctx = new UsersContext())
          {
              User user = ctx.Users.Find(UsersTable.SelectedIndex);
  
              if (user != null)
              {
                  user.Name = txtUName.Text;
                  user.IdNumber = txtIdNumber.Text;
                  user.Department = txtDepartment.Text;
                  user.Position = txtPosition.Text;
                  user.Username = txtUsername.Text;
                  user.Password = pwbPassword.Password;
              }
  
              ctx.Entry(user).State = System.Data.Entity.EntityState.Modified;
              ctx.SaveChanges();
  
              txtUName.Text = "";
              txtIdNumber.Text = "";
              txtDepartment.Text = "";
              txtPosition.Text = "";
              txtUsername.Text = "";
              pwbPassword.Password = "";
  
              UsersTable.ItemsSource = ctx.Users.ToList();
          }
      }
  }
  </code></pre>
  
  <p>The problem is when I select and try to update the first entry to the database (the first row in listview), I get the "ArgumentNullException was unhandled" error/exception.</p>
  
  <p>Visual Studio indicates that the error comes from:</p>
  
  <pre><code>ctx.Entry(user).State = System.Data.Entity.EntityState.Modified;
  </code></pre>
  
  <p>Also, I can't save changes on my last row because when I select it the changes are saved on the row above it.</p>
  
  <p>I would really appreciate some help.   </p>