Tableadapter delete

I have a vb.net forms app that is using a infragiatics grid control that is bound to a typed data set

There is a foreign key relationship between the campaign table and the coupon table based on campaign id

If I delete the coupons (child) first, then try to delete the campaign.  all works well

Would I would like to do is allow the user to delete the campaign, and have all the records associated with it as well
I thought the updateall would take care of this for me but I am getting a foreign key constraint violation
telling me that can't delete campaign because there are associated coupons.

I'm guessing I would have to call the tableadapter delete for all the coupons first but not sure how to determine if the parent is the one being deleted so I know to delete children or if it is children being deleted in which I am ok

Hope this makes sense


 Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
        Me.Validate()

        Me.TblMarketingCampaignBindingSource.EndEdit()



        Me.TableAdapterManager.UpdateAll(Me.MKTG_CampaignDataSet)



        Me.TblMarketingCampaignUltraGrid.UpdateData()
    End Sub





screen_shot.pngscreen_shot.png
johnnyg123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
You can specify ON DELETE CASCADE for CampaignID Foreign Key in Coupon table as decribed here. So whenever a Campaign is deleted, all related Coupons are automatically deleted as well.

Assuming there is a Foreign Key constraint named FK_Campaign_Coupon in Coup table, you can add ON DELETE CASCADE by:
ALTER TABLE [Coupon]  DROP CONSTRAINT FK_Campaign_Coupon

ALTER TABLE [Coupon]  ADD CONSTRAINT FK_Campaign_Coupon  FOREIGN KEY (CampaignID) REFERENCES [Campaign] ON DELETE CASCADE

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnnyg123Author Commented:
Thanks!

Had to add in table and on the xsd make sure to edit relationship to create both relation and foreign key constraint and change delete rule to cascade
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.