[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

C# Dataset Relationships and adding parent rows.

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):

tbl_events
EventID (primary)
EventName

tbl_resources
ResourceID (primary)
ResourceName

tbl_event_resources
EventResourceID (primary)
ResourceID
EventID

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)
    {
        ResourcesListView.Items.Add(resourceRow["ResourceName"].ToString());
    }
}

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
EventDetailsDataSet.tbl_event_resources.Addtbl_event_resourcesRow(
     Convert.ToInt32(resourceId.ToString()),
     Convert.ToInt32(mEventID.ToString()),
     0.0,
     false,"",false);

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

Key:
EventDetailsKey1

Child Element:
tbl_resources

Key Fields,Foreign Key Fields:
EventResourceID, ResourceID

Anyone have any idea what I am doing wrong?


0
protemus
Asked:
protemus
1 Solution
 
protemusAuthor Commented:
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?
0
 
dunglaCommented:
I think you must set Key Fields and Foreign Key Fields both to ResourceID, not EventResourceID
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now