Duplicate records being recorded

  • Thread starter Thread starter Martin Williams
  • Start date Start date
M

Martin Williams

Still working out a few bugs in a survey program I'm writing. The way I
have it configured is as each survey is completed, the dataset is updated.
When the program is closed, the database itself is updated. However, when I
check the database table, there are some answers that are duplicated two and
three times.

Here are some snippets...(VB.NET, 1.1)

//This sub is called on the last page of the survey when the "Finish" button
is clicked.
Sub GatherAndUpdateAnswers()
//there is some code before this that gathers all the answers and assigns
them to variables
dim newrecord as datarow = mainform.dssurveydata1.survey_data.newrow
newrecord(0) = QuestionOneAnswer
newrecord(2) = QuestionTwoAnswer
//repeats like this until twelve

mainform.dssurveydata1.survey_data.rows.add(newrecord)

end sub

Sub UpdateData()
dim pdsInsertedRows as system.data.dataset
pdsInsertedRows = mainform.dssurvey_data1.getchanges(datarowstate.added)
mainform.oledbdataadapter1.update(pdsInsertedRows)
end sub

Private Sub Form1_Closing(...)
Call UpdateData()
end sub

When the "Finish" button is clicked on the last form,
GatherAndUpdateAnswers() is called.

Another problem I am having is Access cutting off some of the fields that
require longer answers. I have these fields set to "Memo" in Access, but
it's still cutting off after 255 characters. Any idea on how to correct
this? Thanks for all responses.
 
Martin - I don't know why you're getting duplicate values but two things
come to mind - first, a Key can ensure that it doesn't happen and that's one
of themain jobs of a key. Irrespective of what's going on on the client
side - you probably want to put something on the db side that ensures that
you don't get junk data in there.

My guess is that you aren't calling AcceptChanges on the source Dataset in
UpdateData - and it's getting called when the user hits Finish, then again
when closing event fires. Because those changesstill exist in the source
dataset until you call Acceptchanges, then they'll get posted twice.

Don't hit the finish button once and see if it still happens - or just call
AcceptChanges on the source dataset right after you call update - my guess
is that this is your problem.

The only other way I can think of to diagnose the problem is to Check the
count of how many changed records you have before they are sent back to the
db. You're problem could be with the bindings or something else unrelated
to this code snippet - but by verifiying that you meant to change two rows
and you have two changes, you can narrow down where the problem is
happening.
 
Back
Top