Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / October 2006

Tip: Looking for answers? Try searching our database.

Master/Detail Relationship

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chuck P - 23 Oct 2006 22:31 GMT
I have a dataset (dsIntake) in the designer with a master/detail relation.
The relation is marked with Relation Only and Nested Relation

In the designer code it is:
this.relationIntakes_vwIntakeRelatedCases = new
System.Data.DataRelation("Intakes_vwIntakeRelatedCases", new
System.Data.DataColumn[] {
                       this.tableIntakes.IntakeIDColumn}, new
System.Data.DataColumn[] {
                       this.tablevwIntakeRelatedCases.IntakeIDColumn},
false);

In my save event I have

 intakesTableAdapter.Update(dsIntake);  //master
 vwIntakeRelatedCasesTableAdapter.Update(dsIntake);  //detail

To test I added one row to the master and one to the detail and tried to
save it.

If I debug break after the first update and look at the dataset.
I see the Intakes table does have the PrimaryKey (IntakeID) column is filled
with the correct value from the database (autonumber).  If I look at the
detail table vwIntakeRelatedCases, the PrimaryKey from the master table
(IntakeID) column is 0.

When I step through the second update I get an exception:
{"The INSERT statement conflicted with the FOREIGN KEY constraint
\"IntakeRelatedCases_IntakeID_fk\". The conflict occurred in database
\"WITS\", table \"SR.Intakes\", column 'IntakeID'.\r\nThe statement has been
terminated."}

I don't know how the FK in the detail column is suppose to be populated.

ALTER PROCEDURE [SR].[Intakes_Ins]
(
    @DateOfIntake datetime,
    @IncidentSummary varchar(2000)
)
AS
    SET NOCOUNT OFF;
INSERT INTO [SR].[Intakes] ([DateOfIntake], [IncidentSummary]) VALUES
(@DateOfIntake, @IncidentSummary);
   
SELECT * FROM SR.Intakes WHERE (IntakeID = SCOPE_IDENTITY())

================================================

ALTER PROCEDURE [SR].[IntakeRelatedCases_Ins]
(
    @IntakeID int,
    @CaseID varchar(20)
)
AS
    SET NOCOUNT OFF;
DECLARE @WitsID int

SELECT @WitsID = (SELECT WitsID FROM SR.Cases WHERE CaseID = @CaseID)

if @WitsID is null
    begin
    raiserror('CaseID does not exist',11,1)
   return
    end
else
    begin
    INSERT INTO [SR].[IntakeRelatedCases] ([IntakeID], [WitsID]) VALUES
(@IntakeID, @WitsID);

    SELECT * FROM SR.vwIntakeRelatedCases WHERE (IntakeRelatedCaseID =
SCOPE_IDENTITY())
    end
====================================================
Linda Liu [MSFT] - 24 Oct 2006 10:38 GMT
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.
Chuck P - 24 Oct 2006 14:30 GMT
I thought the pages of code hidden in the dataset designer was going to
automatically set the foreign key.

> Hi Chuck,
>
[quoted text clipped - 78 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT] - 25 Oct 2006 09:26 GMT
Hi Chuck,

Thank you for your prompt response.

The dataset.Designer.cs file contains the code for the DataSet. It is
consistent with what you see in the designer.

Is the problem solved now? If not, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.