I am using ADO.Internet having a strongly typed dataset in C# (.Internet 3.5). I wish to place a brand new row to 2 tables that are related within an 1:n relation.
Attachments supports the primary key area of the relation and also the table
LicenseAttachments supports the foreign major factor.
AttachmentsDataSet.InvoiceRow invoice; // Set to a valid row, also referenced in InvoiceAttachments AttachmentsDataSet.AttachmentsRow attachment; attachment = attachmentsDataSet.Attachments.AddAttachmentsRow("Name", "Description"); attachmentsDataSet.InvoiceAttachments.AddInvoiceAttachmentsRow(invoice, attachment);
Obviously initially when i first update the
InvoicesAttachments table, I'll obtain a foreign key breach in the SQL server, and so i attempted upgrading the
Attachments table first, developing the rows, and can take away the attachment association within the
InvoiceAttachments table. Why?
How do you solve this issue?
Around the relation between your tables, be sure that the "Both Relation and Foreign Key Constraint" is chosen and "Update Rule" is placed to "Cascade". Combined with "Refresh the information table" option around the adapter, once you place your parent row, the up-to-date ID will "Cascade" lower the associations, stopping foreign key violations inside your dataset. Your son or daughter tables will anticipate to correctly place in to the database.
Several things to test:
Whenever you configure the tableadapter, have you click advanced options, and appearance on "refresh data table" to ensure that it'll retrieve the identity column value?
For me personally sometimes I either didn't remember to check on it, or it did not save the configuration properly because I did not have my table identity increment/seed set for reasons uknown. Are you currently using identity increment up for grabs?
You could also consider just re-creating the plugs for individuals two tables.
Usually after i retrace everything I've found it had been something stupid on my small part.
Lastly, you may consider calling update around the Primary table, then by hand grab the main key value and by hand set the worthiness whenever you place the kid record. In the event that does not seem sensible tell me and that i will publish code.
You have to inform your parent table's table-adapter to refresh the data-table after update operation. This is the way it can be done.
Open the qualities of ProgramUserGroupTableAdapter -> Default Choose Query -> Advnaced options. and appearance a choice of Refresh the information table. Save the adapter now. Now whenever you call update on table-adapter, the data-table is going to be up-to-date [rejuvenated] following the update operation and can reflect the most recent values from database table. when the primary-key or any coloumn is placed to auto-increment, the information-table may have individuals latest value publish recent update.
You can now Call the update as pug.Update(dsUserGroup.ProgramUserGroup)
Read latest values in the ProgramUserGroup coloumns and assign particular values in to the child table before update. This works exactly how you want.