Question Dataset: Inserting Parent/Child rows?

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
I have had several posts on here trying to figure this out. I am a little closer, but still can't figure this out. I am on my last nerve trying to figure this out. I know I am so close. The following two pictures show the part of the dataset diagram that relates to this and the other is a picture of the form i am working with. The details on the left of the form are from my main table, the grid on the left is the child table. As you can see, i get the -1 temp key on the left, which then flows down to the child form. I cant seem to get the child rows to save when i click the save button.

Form:
7-10-2013 2-52-17 PM.jpg

Dataset Designer:
7-10-2013 3-10-30 PM.jpg

As you can see, it is a many to many relationship between flightplan and charge numbers.

Code when clicking save:
VB.NET:
    Private Sub FlightplanBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FlightplanBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.FlightplanBindingSource.EndEdit()
        Me.FlightchargeBindingSource.EndEdit()

        Me.TableAdapterManager.UpdateAll(Me.FlightTestApp2DataSet)

    End Sub

The error i get on the updateall line is:
The INSERT statement conflicted with the FOREIGN KEY constraint "fk_flightcharge". The conflict occurred in database "FlightTestApp2", table "dbo.flightplan", column 'flightplan_id'.
The statement has been terminated.


What am I doing wrong? How does the flightplan_id trickle down to the grid. It should be done through the relation, correct?

Can anyone help me get this working, I just cant figure it out. Any help will be greatly appreciated. Small compensation to anyone that can help me get this working the way i need.
 
I gave up on your previous threads because I'd already shown you one working sample and you seemed not to be able to use the information you already had. This appears to be more of the same. Firstly, I seem to recall that there was talk of both Access and SQL Server in your previous threads. What database are you using now?
 
I responded on that threads but never heard back. I have to screen cap vids that i will attach in a bit that shows you exactly my problem. In my other thread i added attached a solution, but never heard back. were you able to view it.
 
Like I said, I gave up because I felt that I'd already provided enough information. You had a working sample, after all. As for this thread, I did ask a question in my previous post.
 
I am using sql server. Access was never involved. I understand the sample you had. I can make my app work that way, but that was not the way I need. You were using two datagridviews. That works just fine. It is when I add the parent as details and the child as a datagridview. See attached videos. I did it both way to show you what I mean.
 

Attachments

  • vids.zip
    1.8 MB · Views: 29
I understand the sample you had. I can make my app work that way, but that was not the way I need. You were using two datagridviews. That works just fine. It is when I add the parent as details and the child as a datagridview.
The DataGridViews or otherwise are irrelevant. They are just about the display of data and have nothing to do with retrieval, storage or saving. If you have a DataSet containing the same DataTables and the same DataRelations then populating it and saving it are the same no matter what controls you use to display the data to the user. That's the point that you seem to be missing.
 
I get what you are saying, but what you are saying and what is happening are two different things. If you look at those videos, you will see what i mean. The code is the same for both examples. The video with 2 datagridviews shows how it works correctly, but the video where the parent are detail controls, it doesnt work. If what you are saying is true, then why dont they both work. There is no difference in code, the only difference is the parent being added as details instead of datagridview. I understand it shouldnt matter, but there is something happening bc as you can see the one isnt working
 
Everyone shows me how to do it with two dgv's, which I already knows works, bc it works for me, but when I put the parent table on the form as details, it doesnt work.
 
And as I keep saying, the UI is irrelevant. You still have a DataSet with DataTables and a DataRelation and that's all that matters. You retrieve data into the DataSet and you save data from the DataSet. How you display data to the user is irrelevant. If it works differently for you then you're doing something different and it's not related specifically to the UI. The first thing that comes to mind is that the changes made in the UI are not being committed to the underlying DataTable. To ensure that happens, you should be binding your data via a BindingSource and calling EndEdit on that before saving. Some changes will be committed automatically but the current edit may not, regardless of the UI.
 
I understand that. Did you look at the videos? The code behind them is exactly the same. It is just the UI that changed for the parent table.
 
jmcilhinney - I am sorry if i am coming off as an ass. I don't mean to be. I am on the same page as you that it shouldnt matter about the UI.
This is the code behind both forms:
VB.NET:
Public Class Form2

    Private Sub FlightplanBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FlightplanBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.FlightplanBindingSource.EndEdit()
        Me.FlightchargeBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.FlightTestApp2DataSet)
    End Sub

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.FlightchargeTableAdapter.Fill(Me.FlightTestApp2DataSet.flightcharge)
        Me.FlightplanTableAdapter.Fill(Me.FlightTestApp2DataSet.flightplan)

    End Sub
End Class

These are the two forms. I added the controls the same way for both, except one has the parent table as details and not dgv.
7-23-2013 7-43-17 AM.jpg
7-23-2013 7-44-12 AM.jpg

From what you have said, it shouldnt matter what the UI is, which makes sense to me. I agree with that statement. But why when adding a new item does one work and the other does not.
 
Not to be mistaken, this is for inserting a new record/row. Not for loading, updating, etc. This is for new. I am not sure if that makes a difference
 
Back
Top