How do I save data to Access database using a dataset and table adapter?

Saga 461 Reputation points
2022-02-01T22:52:19.623+00:00

Hi all, I am using Visual Studio 2015 with Access 2013 DB (accdb file). I am exploring different ways to interact with the DB and found this video:

C# Application - Insert Delete Update Select in MS Access Database | FoxLearn
https://www.youtube.com/watch?v=uONQaT-nwls

I created a test form with three fields: First and lats name, and email. I added a new data source and followed the video where applicable.

When I click the Save button it saves the data correctly, but only the fields that are bound to the UI elements (text boxes).

Where I am having an issue is saving additional data, such as the name of the data capture person who created this record. I have a CreatedBy text column in the DB, but the name that I assign to it does not save to the DB.

Here is some code:

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

            membersBindingSource.DataSource = this.appDataDataSet.Members;

            //This indicates that I will capture a new record.
            this.appDataDataSet.Members.AddMembersRow(this.appDataDataSet.Members.NewMembersRow());
            membersBindingSource.MoveLast();

        }



        private void button1_Click(object sender, EventArgs e)
        {

            //Save new record to DB.
            try
            {
                membersBindingSource.EndEdit();

                //Assign initials of user that created this record. (Hardcode for test.)
                this.appDataDataSet.Members.CreatedByColumn.DefaultValue = "SMD";

                //Update with new data in app Data DataSet.
                membersTableAdapter.Update(this.appDataDataSet.Members);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                membersBindingSource.ResetBindings(false);
            }
        }

Except for the public frmTest() routine the above code is all the code I have. Obviously, I am doing something wrong, but I can't determine what. The above code correctly saves the first and last name, and the email, but "ignores" the CreatedBy statement. I am currently looking for information on how to do this. Any orientation would be greatly appreciated! Thanks! Saga

Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | 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.
{count} votes

Answer accepted by question author
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2022-02-04T09:26:35.137+00:00

    You can't use default value for this, instead subscribe to DefaultValuesNeeded for the DataGridView

    171275-figure2.png

    Code where CreatedByColumn is the DataGridView column name for created by in this case.

    private void personDataGridView_DefaultValuesNeeded(  
        object sender, DataGridViewRowEventArgs e)  
    {  
        e.Row.Cells["CreatedByColumn"].Value = Environment.UserName;  
    }  
    

    Make sure when testing you set copy to output directory to

    171334-copy.png

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2022-02-02T01:38:04.713+00:00

    Set the default value in the designer which is where it should be done for a constant value.

    170393-figure1.png

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.