Click here to Skip to main content
15,891,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables in Access, table1 with Primary key and table2 with foreign key.
I Created a WIn form in C#. Drag both tables into forms with details. table2 as a it appears at the bottom of table1. Using BindingNavigator i created a new empty form. Enter data and try to save the data into table 1 & 2. It Saves in table1 BUT not in table2. In fact when I click save button, it erases the content of all controls related to table2
Posted

1 solution

The drag and drop contorl won't do this for you.

You have to add the code to save the data to table2 yourself. You'd write the data to table1, get the primary key value of the newly added record, then write the data to table2, using the key value from table1 as the foreign key.

You can find an example of doing that here[^].
 
Share this answer
 
Comments
Member 9054838 31-May-12 11:21am    
Many thanks. i already saw the example and here is my code: It doesn't save. here is my code:
private void table1BindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.table1BindingSource.EndEdit();
this.table2BindingSource.EndEdit();

dbaccess1DataSet.table2DataTable modifiedtable2 = (dbaccess1DataSet.table2DataTable)
dbaccess1DataSet.table2.GetChanges(DataRowState.Modified);
dbaccess1DataSet.table2DataTable deletedtable2 = (dbaccess1DataSet.table2DataTable)
dbaccess1DataSet.table2.GetChanges(DataRowState.Deleted);

dbaccess1DataSet.table2DataTable newtable2 = (dbaccess1DataSet.table2DataTable)
dbaccess1DataSet.table2.GetChanges(DataRowState.Added);

if (modifiedtable2 != null)
{
table2TableAdapter.Update(modifiedtable2);
}
if (deletedtable2 != null)
{
table2TableAdapter.Update(deletedtable2);
}
if (newtable2 != null)
{
table2TableAdapter.Update(newtable2);
}

dbaccess1DataSet.AcceptChanges();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900