Master Detail entering new Master + new Detail(s)???

  • Thread starter Thread starter Grant Schenck
  • Start date Start date
G

Grant Schenck

Hello,

This is for Win Forms.

I have a simple master detail form with two datagridviews. Top view has a
list of "Station Lists" showing an ID and Name and the bottom is a list of
the "Stations" for the station list selected in the top. Currently, aside
from the FK to the station list ID the only other column is a text field
called "Extension".

I created the app with the wizards and then modified my bindings for the
"Station" grid view's bind source to set the datasource to the master's
(StationList) binding source and the datamember the relation created by the
wizards.

I also added an update button to the form which when I click I then make
these calls:

this.stationListTableAdapter.Update(this.sTPSRecorderDataSet);
this.stationTableAdapter.Update(this.sTPSRecorderDataSet);

This all works fine. I can select an existing "Station List" in the top
grid and then enter a new Station extension in the bottom grid and it sets
the StationListID to the station list selected in the top grid. If I then
click my update button the changes are committed to the SQL DB.

The problem I have is if I enter a new station list name in the top grid
the station list ID shows as -1 and if I then enter an extension in the
bottom it lets me do so also showing the same -1 as the FK in the bottom.
However, if I then click my update button the first update works but the
second throws an exception:

{"The INSERT statement conflicted with the FOREIGN KEY constraint
\"FK_Station_StationList1\". The conflict occurred in database
\"STPSRecorder\", table \"dbo.StationList\", column 'StationListID'.\r\nThe
statement has been terminated."}

I'm thinking I need to make some call after I update the station list table
but before I update the station table but if so I'm not sure what.

Any ideas?

Thanks!
 
Back
Top