Concurrency question

B

Bob

While testing my my program I came up with a consistency exception. My
program consists of three datagridviews, One called dgvPostes which is the
parent grid and its two children,one called dgvPlans and the other dgvTanks.
What happens is as follows. I will either create or edit a record in the
datagridview dgvPlans and call the Updatedb procedure (code below). The
first save works OK. Then when that is done, on the same record I will try
to edit it and do another UpdateDB. That updatedDB execution will catch a
concurrency exception error. But there is no concurrent use of the database.
I am the only one who is using it and updating the records. The code
structure that I used below is straight out of the Microsoft sample on their
web site. I went back to the dataset designer and changed the properties to
exclude (uncheck) the optimistic concurrency checking when creating the
datatable definitions. Then I tried my code again and I no longer got the
Concurrency exception. From what I see, if you do NOT check the optimistic
concurrency checking there is NO concurrency checking at all. On the other
hand if you do check it, it doesn't seem to work right since the changes
that it caught as concurrency violations were in fact ok.

This is the code snippet of my updatedb method. Can someone please tell me
if I should do something to it to avoid the concurrency exception on the
second edit-save. It occurs on the line
DsPlansEtCheminsPostes.AcceptChanges()



Private Sub UpdateDB()

Me.Validate()

Me.FKReservoirsPostesPostesBindingSource.EndEdit()

Me.FKPostePlansPostesBindingSource.EndEdit()

Me.PostesBindingSource.EndEdit()

'We can only update the database's PostesPlans table and the field that is
bound to the cheminPoste

'in table poste.

'if we had deleted any records in posteplans, find them

Dim deletedChildRecordsPostePlans As
dsPlansEtCheminsPostes.PostePlansDataTable = _

CType(DsPlansEtCheminsPostes.PostePlans.GetChanges(Data.DataRowState.Deleted),
dsPlansEtCheminsPostes.PostePlansDataTable)

'We do not need to verify the reservoirsPostes part of this because we can
never change them

'If we had deleted any poste plans, find em

Dim newChildRecords As dsPlansEtCheminsPostes.PostePlansDataTable = _

CType(DsPlansEtCheminsPostes.PostePlans.GetChanges(Data.DataRowState.Added),
dsPlansEtCheminsPostes.PostePlansDataTable)

'If we had modified any child records, get them

Dim modifiedChildRecords As dsPlansEtCheminsPostes.PostePlansDataTable = _

CType(DsPlansEtCheminsPostes.PostePlans.GetChanges(Data.DataRowState.Modified),
dsPlansEtCheminsPostes.PostePlansDataTable)

Dim modifiedChildRecordsReservoirs As
dsPlansEtCheminsPostes.ReservoirsPostesDataTable = _

CType(DsPlansEtCheminsPostes.ReservoirsPostes.GetChanges(Data.DataRowState.Modified),
dsPlansEtCheminsPostes.ReservoirsPostesDataTable)

'Note we do not have any possibility of adding or deleting reservoirspostes
records in this application.

'So we don't have to check for new or deleted records, we will only check
for modified records.

Try

If deletedChildRecordsPostePlans IsNot Nothing Then

Me.PostePlansTableAdapter.Update(deletedChildRecordsPostePlans)

End If

Me.PostesTableAdapter.Update(DsPlansEtCheminsPostes.Postes)

If newChildRecords IsNot Nothing Then

Me.PostePlansTableAdapter.Update(newChildRecords)

End If

If modifiedChildRecords IsNot Nothing Then

Me.PostePlansTableAdapter.Update(modifiedChildRecords)

End If

If modifiedChildRecordsReservoirs IsNot Nothing Then

Me.ReservoirsPostesTableAdapter.Update(modifiedChildRecordsReservoirs)

End If

DsPlansEtCheminsPostes.AcceptChanges()

Me.Cursor = Cursors.Default

MsgBox(My.Resources.msgSaveOK)

Catch ex As Exception

Me.Cursor = Cursors.Default

EM.HandleException(ex, False, My.Resources.msgNoSave,
My.Resources.msgActionNotPerformed, My.Resources.msgTryAgain)

Finally

Me.Cursor = Cursors.Default

If deletedChildRecordsPostePlans IsNot Nothing Then

deletedChildRecordsPostePlans.Dispose()

End If

If newChildRecords IsNot Nothing Then

newChildRecords.Dispose()

End If

If modifiedChildRecords IsNot Nothing Then

modifiedChildRecords.Dispose()

End If

End Try

End Sub

Thanks for your help,
Bob
 
C

Cor Ligthert [MVP]

Bob,

It is so much code, that I cannot really see everything in this message
however the update sequence is in my idea.

Delete Child records
Do parent updates
Do Child added and modified updates.

I thought that is not the sequence you are using.

If you have "on delete cascade" in your database for these than you can even
forgot the first step.

Cor
 
B

Bob

Thanks

Cor Ligthert said:
Bob,

It is so much code, that I cannot really see everything in this message
however the update sequence is in my idea.

Delete Child records
Do parent updates
Do Child added and modified updates.

I thought that is not the sequence you are using.

If you have "on delete cascade" in your database for these than you can
even forgot the first step.

Cor
 
B

Bob

Just looked at my code again. Thats exactly the sequnce that I'm using, so
why the concurrency problem?
Regards,
Bob
 
C

Cor Ligthert [MVP]

Bob,

And you are sure that the rowstate of the deleted child records that should
go with the deleted parents is set (Or your database has on delete cascade
on the key of that?)

Cor
..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top