Results 1 to 4 of 4

Thread: FK error on Datagridview

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    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:

    Attachment 156919

    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:

    Attachment 156921


    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:

    Attachment 156923

    Can someone please see what I am do wrong?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    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

Posting Permissions

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



Click Here to Expand Forum to Full Width