dcsimg
Results 1 to 4 of 4

Thread: FK error on Datagridview

  1. #1

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    829

    FK error on Datagridview

    Hi, I have be in similar situation before. Almost I think.

    I have tables with one to many relations. So two of them tables in the form of datagridviews. I have changed relationships to cascade on update and delete rule like follows:

    Name:  Capture1.jpg
Views: 87
Size:  39.7 KB

    Then I have made sure that the tableAdapters is fill in correct order when form loads. But now I still get this FK error. This error is even before try to save. If I type in the TblCausesDataGridView and the moment I move to TblControlsDataGridView then this error is show:

    Name:  Capture.JPG
Views: 76
Size:  74.1 KB


    This is the insert command of the tableAdapter:
    Code:
    INSERT INTO [dbo].[tblCauses] ([RiskID], [Cause]) VALUES (@RiskID, @Cause);
    SELECT CauseID, RiskID, Cause FROM tblCauses WHERE (CauseID = SCOPE_IDENTITY())
    This is the DataSet design view:

    Name:  Capture3.jpg
Views: 74
Size:  13.3 KB

    Can someone please see what I am do wrong?
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,674

    Re: FK error on Datagridview

    Is this happening when you add a new record to the child grid? If so then it's up to you to set the foreign key column in that new record. It won't get set automatically, even if the child grid is filtered based on the selection in the parent grid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    829

    Re: FK error on Datagridview

    hi... this first table (tblRiskAssessment) is in "details" on the form. tblCauses and tblControls is in datagridviews. If I type in textboxes for tblRiskAssessment and move to tblCauses no problem. But if I type in CausesDataGridView and move to PControlsDataGridView then it gives the error. It happens the moment I move away from CausesDataGridView
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  4. #4

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    829

    Re: FK error on Datagridview

    Okay I think I figured out.. I need to EndEdit on the bindingsources... It seems to solve the problem. But I must test more



    Code:
    Private Sub TblCausesBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TblCausesBindingSource.AddingNew
            Me.TblRiskAssessmentBindingSource.EndEdit()
        End Sub
    
        Private Sub TblPControlsBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TblPControlsBindingSource.AddingNew
            Me.TblCausesBindingSource.EndEdit()
        End Sub
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width