C# Dataset Relationships and adding parent rows.

Posted on 2005-04-10
Last Modified: 2008-02-20
I know this is probably simple, but I have a SQL DB that gets filled into a dataset with 3 tables.  The three tables represent a many to many relationship, two data tables with primary keys and a bridge table.  Basically, it is to track events and resources that are associated with those events.  The schema is as follows (I have eliminated non-relevant fields for clarity):

EventID (primary)

ResourceID (primary)

EventResourceID (primary)

basically, the user selects an event, and I loop through the tbl_event_resources and getChildRows on it to retrieve the resources that are associated with the event.  Here is the code to loop through and add the used resources to a listview:

foreach (DataRow eventResourceRow in EventDetailsDataSet.Tables["tbl_event_resources"].Rows)
    DataRow[] resourceRows;
    resourceRows = eventResourceRow.GetChildRows("tbl_resourcestbl_event_resources", DataRowVersion.Current);
    foreach (DataRow resourceRow in resourceRows)

This code works great (and correctly gives me the resources for the specific event) as long as the dataset is simply filled via an adapter.  However, when I add a row in code, things start going haywire.

long resourceId = ((DataMenuItem)sender).ID; //This number IS the correct number

Now, when the foreach above is run, it will still show me the original child rows, but I either get no additional resources from GetChildRows, or I get one that is not the one I actually added.  I thought at first I had my relationship defined wrong, but in that case the originals wouldn't show up correctly.  

The relationship in the dataset is defined as follows:
Parent Element: tbl_event_resources


Child Element:

Key Fields,Foreign Key Fields:
EventResourceID, ResourceID

Anyone have any idea what I am doing wrong?

Question by:protemus

    Author Comment

    For some reason, the relationship editor forces you to define the primary key of the parent table as one of the keys... is this a bug in the editor, how do I delete akey that has been aded with the "new" button in the relationship editor?
    LVL 13

    Accepted Solution

    I think you must set Key Fields and Foreign Key Fields both to ResourceID, not EventResourceID

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now