Unable to update database while running app second time

John 426 Reputation points
2024-05-06T23:45:21.2033333+00:00

Screenshot (39)

  1. User entered data before exiting the app.

Screenshot (40)

  1. User entered data in sub table before exiting the app.Screenshot (41)
  2. After user exists the app, user runs the app again, the data is not updated.Screenshot (42)
  3. Sub or linked table's data fails to update its data just after user goes to next record.

Here as stated above, the four steps, something needs to be fixed. Already, I set the "copy if newer" drop box in the properties window for the corresponding database file and its schema.

The code snippet for the "Save" control button as stated below:

        private void tenantsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tenantsBindingSource.EndEdit();
            this.addressesBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.namesDataSet);

        }

Although everything ran smoothly, its data won't update after user runs the app second or more times just to ensure the data is updated.

Will anyone assist me in this?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,365 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 35,556 Reputation points Microsoft Vendor
    2024-05-07T09:00:45.65+00:00

    Hi @John , Welcome to Microsoft Q&A,

    Your problem may be that the database data accessed when loading a new form is not updated.

    Your code snippet shows a click event handler for the save button, but it only updates the data bound to tenantsBindingSource and addressesBindingSource. If you want to ensure that the data is updated when the application starts, you can add code for the data update in the form load event.

    You can try adding the following code in the form load event to ensure the data is updated when the application starts:

    private void Form1_Load(object sender, EventArgs e)
    {
         // TODO: This line of code may be slightly adjusted based on your actual data source
         this.tenantsTableAdapter.Fill(this.namesDataSet.Tenants);
         this.addressesTableAdapter.Fill(this.namesDataSet.Addresses);
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. John 426 Reputation points
    2024-05-07T15:05:49.5533333+00:00

    I did the following steps:

    1. Add a record in a table.
    2. Click on the "First Record" icon in a binding navigator data control.
    3. Click on the "Save" button.
    4. Temporary exit the app.
    5. Run the app again; all records are updated before the previous app exit.

    I may need an event handler, a delegate, and a corresponding method that will automatically refer to when a user clicks on the "First Record" and "Save" commands in a binding navigator data control.


  3. Karen Payne MVP 35,196 Reputation points
    2024-05-09T11:07:50.9733333+00:00

    The following can help diagnose issues. The GetChangesButton contains code that will tell you if there are actually modified or added rows while the ListChange event allows you too peek at a row.

    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Windows.Forms;
    
    namespace TableAdapterSimple
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void booksBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                try
                {
                    Validate();
                    booksBindingSource.EndEdit();
                    tableAdapterManager.UpdateAll(this.booksDataSet);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Save failed\n{ex.Message}");
                }
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                categoriesTableAdapter.Fill(booksDataSet.Categories);
                booksTableAdapter.Fill(booksDataSet.Books);
                booksBindingSource.ListChanged += BooksBindingSource_ListChanged;
            }
    
            /// <summary>
            /// Here we can inspect changes and can reject changes dependent
            /// on validation rules.
            /// </summary>
            private void BooksBindingSource_ListChanged(object sender, ListChangedEventArgs e)
            {
                if (e.ListChangedType == ListChangedType.ItemChanged && e.PropertyDescriptor != null)
                {
                    var book = ((DataRowView)booksBindingSource[e.NewIndex]).Row as BooksDataSet.BooksRow;
                }
            }
    
            private void GetChangesButton_Click(object sender, EventArgs e)
            {
                /*
                 * If sitting on a new or modified row, first move off the row.
                 */
                var bookDataTable = booksDataSet.Tables["Books"];
                var modified = bookDataTable.GetChanges(DataRowState.Modified);
                if (modified != null)
                {
                    // have some modified rows
                }
                var added = bookDataTable.GetChanges(DataRowState.Added);
                if (added != null)
                {
                    // have some added rows
                }
            }
        }
    }
    
    0 comments No comments