Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new in programming, making my first project using vb.net and access. i am trying to develop a project in which the data of patients of the is added from different counters. what i developed is working fine when only one counter adds data in database but when two counters access the same database and try to save the record it gives error primary key can not be duplicate.

i doing what, first i am generating a primary key number i.e. patient no that is unique to every patient. patient no. is one increment to the last saved record. then the user enter (counter data entry operator) adds the patient details and then hit the save button.

in multiuser environment both the operators generate the same patient no. when they hit the new record button as both see the same last saved record. while saving the record one operator save the record successfully but other operator get the duplicate primary key error.

Pessimistic and optimistic locks are not working for me or i am not understanding how to use them.

rsS As New ADODB.Recordset rsS.Open(str, conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)

plz someone guide me

What I have tried:

i tried to solve this problem by saving the patient no. in another variable oldPatientno and beore saving the record checked if there is any change in the database if so then regenerate the patient no. but this is not working
Posted
Updated 3-Oct-17 19:45pm
Comments
Richard Deeming 5-Oct-17 14:12pm    
If you're using .NET, don't use the ancient and obsolete ADODB library. Switch to using ADO.NET[^] instead.

1 solution

First off, don't use Access. While it is possible to get Access working in a multi-user environment, it always causes nasty problems.

Instead, use a multiuser database system like Sql Server or MySql which is designed to work with multiple simultaneous users.

Provided you have written your code correctly, it should just be a case of using SqlConnection instead of OdbcConnection, SqlCommand instead of OdbcCommand, and so forth - the actual SQL SELECT, INSERT, and UPDATE commands should need little change.

But while you are doing that, change your DB design. NEVER use "last record plus one" to pregenerate an ID - use an IDENTITY column in your DB instead, and let the database engine sort out the unique value for you - you do not need the ID before the data has been inserted into the DB so ID pregeneration is always a recipe for trouble, as it always gives the problems you are seeing. Locking won't help here, indeed, it will cause more problems, not less.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900