Home > OS >  Table adapter not updating to database
Table adapter not updating to database

Time:09-13

I created a form system that takes in the staff personal data (with the purpose to create an account) the data is correctly taken and entered into the dataset (dataSet2).The line below was what I believed was the correct set of code to update the database (Database1).The data correctly enters the dataset without an issue but I do not see any data entered into the database at all, there are no error codes output or provided. All help is appreciated.

namespace COMP_SCI_2
{
    public partial class staff_createacc : Form
    {
           
        public staff_createacc()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          
            string[]DATAINPUT = { IDBOX.Text ,TITLEBOX.Text,FORENAMEBOX.Text,SURNAMEBOX.Text,DATEBOX.Text,STREETBOX.Text,TOWNBOX.Text,POSTCODEBOX.Text,PHONEBOX.Text,EMAILBOX.Text,POSITIONBOX.Text,PASSWORDBOX.Text };


        dataSet2.STAFF.Rows.Add (DATAINPUT);
        this.sTAFFTableAdapter.Update(this.dataSet2);
        }
   

CodePudding user response:

If a row was added than you ran the app again to display data and nothing shows. This sound like the database resides in the app folder.

The default for the database property Copy to Output Directory is Copy always which means each time the app runs it overwrites the database. You need to change Copy to Output Directory to Copy if newer

See the following Microsoft TechNet enter image description here

EDIT Did not see the other post with the same information when posting this.

CodePudding user response:

Local data files work a specific way in .NET projects. When you add a data file to your project, e.g. MDF or ACCDB, that is a source file. It is part of your project, not part of your application. When you build your project, the output is written to a folder. That output consists of the compiled EXE and, amongst other things, it can include a copy of your source database.

By default, a new copy of the database is created every time a new EXE is compiled. That means that, if you run your project and modify the database, then change the code and run the project again, your changes will be overwritten by a new copy of the source database. If you run the project again without making any code changes, you'll see that your data changes have persisted.

In the Solution Explorer, your data file has a Copy to Output Directory property and it is set to Copy Always by default. That means that the source database is copied over the working database on every build. If you change that to Copy if Newer then the working database will only be overwritten when you make changes to the source database, e.g. change the schema or the initial data. That way, any changes you make while testing will persist across builds and you can just manually delete the working file when you want to force a refresh.

For the record, the reason that it's done this way is so that your source database doesn't get polluted with your test data. If that were allowed to happen, you'd have to clean it up before deploying. The way it's implemented, your simply change to Release, click Build and your Release output folder will be populated with a nice clean database, ready to be deployed to your users.

  • Related