-
Mar 3rd, 2018, 06:54 AM
#1
Thread Starter
Frenzied Member
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?
-
Mar 3rd, 2018, 07:24 AM
#2
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.
-
Mar 3rd, 2018, 08:37 AM
#3
Thread Starter
Frenzied Member
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
-
Mar 3rd, 2018, 08:57 AM
#4
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|