Search code examples
c#sqlxamlado.nettableadapter

TableAdapter Update never gets written to the table


I have the following code to update a user password. My application starts with a MainWindow.xaml which has a login screen and a change password button. The change password button opens the PasswordRecover.xaml but keeps the MainWindow.xaml opened.

In PasswordRecover.xaml user has to enter the current password and the new password plus confirmation for it. To update the user password I use the method btnConfirm_Click_1.

Once the password is changed, returns to MainWindow screen.

When I login the program only accepts the new password, it rejects the old one as it is suposed to, but when the application closes, the user password has not been updated in the table.

Thanks for your help.

using System.Data;
using System.Data.SqlClient;
using System.IO;
using Library.DataBase.dsLibraryTableAdapters;
using Library.DataBase;    

tbUsersTableAdapter tableAdapterUsers = new tbUsersTableAdapter();
dsLibrary.tbUsersDataTable dataTableUsers;

private void btnConfirm_Click_1(object sender, RoutedEventArgs e)
{
     dataTableUsers = tableAdapterUsers.getDataByUserName(lblUser.Content.ToString());
     tbUsersRow = (dsLibrary.tbUsersRow)dataTableUsers.Rows[0];
     tbUsersRow.userPassword = txtNewPassword.Password.ToString();
     tableAdapterUsers.Update(dataTableUsers);
}

Solution

  • After hours looking into this the solution that worked for me is:

    1. Right click in your .mdf file in the Solution Explorer and choose Properties;
    2. Change the setting Copy to Output Directory to "Do not copy";
    3. In App.config edit your Connection String, replace "|DataDirectory|\DataBase\Library.mdf" with the full path to the .mdf file, for example C:\DataBase\Library.mdf.

    Hope this helps someone else in the future!

    Take care.