2

Datagridview not saving changes to Database.

Just in the form but not the database.

I've checked Database Properties and Copy to output Directory is set to cope if newer .

Here is my code :

public partial class ClientsManager : Form
{

    public ClientsManager()
    {
        InitializeComponent();
    }

    private void clientsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        try
        {

            this.Validate();
            this.clientsBindingSource.EndEdit();
            //this.tableAdapterManager.UpdateAll(this.databaseDataSet);
            this.clientsTableAdapter.Update(this.databaseDataSet.Clients);
            MessageBox.Show("Changes has been Saved!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
      }
   }

    private void ClientsManager_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'databaseDataSet.Clients' table. You can move, or remove it, as needed.
        this.clientsTableAdapter.Fill(this.databaseDataSet.Clients);

    }

Most Solutions I've read so far were about EndEdit() and Update() .. I've this in my code and still not working.

Dr ZIZO
  • 333
  • 4
  • 17

2 Answers2

1

You can create a new button named SAVE and put the below code inside it.

yourdatadapatername.update(yourdatatablenamehere)

Hope That helps.

arshboy
  • 11
  • 1
1

I have found the solution in this Answer :

It is a quite common problem. You use the |DataDirectory| substitution string. This means that, while debugging your app in the Visual Studio environment, the database used by your application is located in the subfolder BIN\DEBUG folder (or x86 variant) of your project. And this works well as you don't have any kind of error connecting to the database and making update operations.

But then, you exit the debug session and you look at your database through the Visual Studio Server Explorer. This window has a different connection string (probably pointing to the copy of your database in the project folder). You search your tables and you don't see the changes.

Then the problem get worse. If you have your database file listed between your project files and the property Copy to Output directory is set to Copy Always, then, every time you restart a debug session, the original database file is copied from the project folder to the output folder (BIN\DEBUG) and thus your previous changes are lost. Now, also your application doesn't see the changes made before.

You could stop this problem changing the property Copy To Output Directory to Copy If Newer or Never Copy. Also you could update your connectionstring in the Server Explorer to look at the working copy of your database or create a second connection. The first one still points to the database in the project folder while the second one points to the database in the BIN\DEBUG folder. In this way you could keep the original database ready for deployment purposes and schema changes, while, with the second connection you could look at the effective results of your coding efforts.

EDIT Special warning for MS-Access database users. The simple act of looking at your table changes the modified date of your database ALSO if you don't write or change anything. So the flag Copy if Newer kicks in and the database file is copied to the output directory. With Access better use Copy Never.

Community
  • 1
  • 1
Dr ZIZO
  • 333
  • 4
  • 17