korrawit
korrawit

Reputation: 1000

C# TableAdapter and DataSet update with multiple table

I'm new in C# and I need some help.

enter image description here

I just want to update row in Session table by code below.

        dbDataSet db = new dbDataSet();
        SessionTableAdapter sessionTableAdapter = new SessionTableAdapter();
        sessionTableAdapter.Fill(db.Session);
        var session = db.Session.Where(s => s.idSession.Equals(new Guid("{0F0B0E1A-950E-4BCE-9C68-6D1387EDAC90}"))).SingleOrDefault();
        session.endTime = DateTime.Now;
        sessionTableAdapter.Update(db.Session);

I got an error at "sessionTableAdapter.Update(db.Session)"

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: The record cannot be deleted or changed because table 'PaidLog' includes related records.

How to fix this error ?

Upvotes: 1

Views: 5204

Answers (2)

korrawit
korrawit

Reputation: 1000

Solved !

At first that I just only tick Enforce Referential Integrity.

But I try to tick Cascade Update Related Fields and It's works !

I don't know why it work.

Anyone know the reason about this thing?

By the way, thanks to Maheep.

enter image description here

Upvotes: 0

Maheep
Maheep

Reputation: 5605

The error message says it all. Session table has foreign key relation with PaidLog. You will have to update child rows from this table first.

See this article on MSDN for more details in the "Updating Two Related Tables in a Dataset with a TableAdapter" section

Or you might want to use TableAdapterManager

Upvotes: 3

Related Questions