-1

I just starting out with C# and am trying to make a CRUD for a dataset on a local database. If I run my program everything works great. but once I restart it all my data that I entered in the last session is gone. I read many articles like this and they said to enter : employeeDataSet.AcceptChanges(); but even after that it still doesn't work, any ideas?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LocalDatabaseApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    private void employeeInfoBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.employeeInfoBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.employeeDataSet);

    }

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

    }

    private void addButton_Click(object sender, EventArgs e)
    {
        this.employeeInfoBindingSource.AddNew();
        employeeDataSet.AcceptChanges();
    }

    private void saveButton_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.employeeInfoBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.employeeDataSet);
        employeeDataSet.AcceptChanges();
    }

    private void removeButton_Click(object sender, EventArgs e)
    {
        this.employeeInfoBindingSource.RemoveCurrent();
        employeeDataSet.AcceptChanges();
    }
}

}

A screenshot enter image description here

Rodney Wormsbecher
  • 897
  • 2
  • 17
  • 39
  • Can you explain "local database"? Is that SQL Compact Edition or something else? – Fatih TAN Aug 09 '16 at 09:57
  • it's sqlLocalDb in visual studio 2015 i believe its called service database. the file extention is .mdf Furthermore it doesn't matter whether I use my own buttons or the original datasource control buttons that come when you add the data source to the form. In both cases it won't update – Rodney Wormsbecher Aug 09 '16 at 10:09

2 Answers2

1

After reading the following post: Why saving changes to a database fails?

I figured out how to solve this problem. In the solution explorer -> right click the database file -> properties -> Copy to Output directory -> change to "Copy if newer".

This effectively copies any changes made to the database when you close the application.

Community
  • 1
  • 1
Rodney Wormsbecher
  • 897
  • 2
  • 17
  • 39
  • Actually this setting makes sure that the database file included with the project is only copied to \bin\debug if it is newer than the file that already is in \bin\debug. I suspect you're using the wrong database file. You should be changing the file that's in \bin\debug, not the one that is in your project's folder. What's your connection string? I think you're getting the wrong picture of how things work, really... – Thorsten Dittmar Aug 09 '16 at 10:42
0

AcceptChanges doesn't store the data back to the database. It simply internally marks modified records as "unchanged". It clears the "dirty" flag for all the added/removed/modified records in the data set so that it appears to be unchanged.

You need to remove the calls to AcceptChanges and instead call TableAdapter.Update(DataSet).

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Did you remove the `AcceptChanges` calls? Otherwise "Update" will not have anything to update. Also you should check the INSERT, UPDATE and DELETE commands of the table adapter. – Thorsten Dittmar Aug 09 '16 at 10:12
  • yes I have deleted all the acceptChanges lines and changed them into this.tableAdapterManager.UpdateAll(this.employeeDataSet); as there was no single update only updateAll function. The sql all looks fine. So I am starting to doubt its somethign witht he database instead of the code. – Rodney Wormsbecher Aug 09 '16 at 10:30
  • 1
    If there was a problem with the database you'd be getting an exception. Are you sure you're not overwriting your MDB file every time your application starts? Are you sure you're reading from/writing to the same MDB file? – Thorsten Dittmar Aug 09 '16 at 10:31
  • thanks for the help mate. After reading the following post: Why saving changes to a database fails? I figured out how to solve this problem. In the solution explorer -> right click the database file -> properties -> Copy to Output directory -> change to "Copy if newer". This effectively copies any changes made to the database when you close the application. – Rodney Wormsbecher Aug 09 '16 at 10:36
  • Still, `AcceptChanges` doesn't save anything. You do need to call `Update(All)`, so my answer is valid :-) – Thorsten Dittmar Aug 09 '16 at 10:43