"Linda Liu [MSFT]" wrote:
> Hi Chuck,
>
> > I don't know how the FK in the detail column is suppose to be populated.
>
> The FK contraint is violated at the database level in this scenario. The
> exception occurs because there is no value 0 of the IntakeID column in the
> master table while you are attempting to insert a new row with value 0 of
> the IntakeID column into the detail table.
>
> If you mark the relation between the two datatable in the dataset with
> 'Both Relation and Foreign Key Constraint' or 'Foreign Key Constraint
> Only', an exception will occur even when you add such a new row with value
> 0 of the IntakeID column into the detail datatable, because FK constraint
> is enabled in the dataset level.
>
> It seems that the IntakeID column in the master table is auto-increment,
> which means that only after the new row is saved in the database, the
> IntakeID column gets a real value from the database. We should get the
> value of the IntakeID column at this time and set it to the new row of the
> detail datatable.
>
> We have two options to do it. One option is to get the value of the
> IntakeID column from the new added row directly and the other option is to
> set the parent-child relationship between the parent row and child row. As
> for the option 2, the DataRelation will automatically cascade the primary
> key value down to the related child rows. The following is the sample code
> for the two options.
>
> // The datatable 'State' is a master table and 'City' is a detail table
> // The third column in the City is the foreign key
>
> DataRow stateRow = this.dataSet11.State.NewRow();
> stateRow[1] = "aaa";
> this.dataSet11.State.Rows.Add(stateRow);
>
> DataSet1TableAdapters.StateTableAdapter stateAdapter = new
> DataSet1TableAdapters.StateTableAdapter();
> stateAdapter.Update(stateRow);
>
> DataRow cityRow = this.dataSet11.City.NewRow();
> // option 2 of setting a relation between the parent row and child row
> cityRow.SetParentRow(stateRow);
> cityRow[0] = 1;
> cityRow[1] = "bbb";
> // option 1 of getting the value of the primary key directly and setting it
> into the child row
> cityRow[2] = stateRow[0];
> this.dataSet11.City.Rows.Add(cityRow);
>
> DataSet1TableAdapters.CityTableAdapter cityAdapter = new
> DataSet1TableAdapters.CityTableAdapter();
> cityAdapter.Update(cityRow);
>
> this.dataSet11.AcceptChanges();
>
> Hope this helps.
> If you have anything unclear or need our further assistance, please feel
> free to let me know.
>
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
>
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif > ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
>
http://msdn.microsoft.com/subscriptions/support/default.aspx. > ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>