primary key problem !!! what to do?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i made VB.NET/Access application , app is working on network ok but i have one problem, i made system in witch i use primary key (autonumber) as starting point for dokument i use in app, but problem starts when i have two pipl (two computers) and they access database in same time , then they have same document number becouse primary key is the same becouse in that time record is not created yet so what to do ? i need to access database give any client difrent id ? what to do maybe i have wrong idea of resolving this

thanks

Vojislav Milutinovi
 
Take a look at this...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/adonetbest.asp

....and do a search for '-1'

The basic idea is that autoincrement fields local to the machine
go -1, -2, -3 but that they get updated with the correct, central database
generated value, when you push the new rows back to the server.


Voya said:
i made VB.NET/Access application , app is working on network ok but i have
one problem, i made system in witch i use primary key (autonumber) as
starting point for dokument i use in app, but problem starts when i have two
pipl (two computers) and they access database in same time , then they have
same document number becouse primary key is the same becouse in that time
record is not created yet so what to do ? i need to access database give any
client difrent id ? what to do maybe i have wrong idea of resolving this.
 
we have part of solution, problem is becouse when put new record in dataset i get new key and i use it in app but when i update it key will change and i get problems , ... if i add record with id = 1 and then update it and in midle time some one update some record before me it will get id =1 , that means if i update next i will get id =2 , but i allready put some 'calculations' with id = 1 and problem ocures. i use id to make number of document unike .... so is it posible to make same dataset for two app on two computers or for access to distribute id key with some logic ????

thank
 
You are doing something fundamentally wrong as an update should not be
changing a primary key. Once assigned they remain assigned if for no other
reason than primary keys are used as references (foreign keys) in associated
records. You should not be changing them.

Voya said:
we have part of solution, problem is becouse when put new record in
dataset i get new key and i use it in app but when i update it key will
change and i get problems , ... if i add record with id = 1 and then update
it and in midle time some one update some record before me it will get id
=1 , that means if i update next i will get id =2 , but i allready put some
'calculations' with id = 1 and problem ocures. i use id to make number of
document unike .... so is it posible to make same dataset for two app on
two computers or for access to distribute id key with some logic ?????
 
Hi Voya,

There are I think more solutions for your problem.

The way I would examine if it fits my problem first (if I understand your
problem well), is that when the user want to start a new "document" as you
name it, to make a new row in the database with that autocreated number.
That empty row is returned with the key. The user can now update it on
screen and than do an update of the database.

One thing that should be remembered with this approach is that when the user
decided not to update, the false created row should be deleted again. (Or it
stays empty and than there can be an garbadge solution made, to do that in
one time, although i see not much benefit from that in this situation).

And as Tom stated, the autonumber is of course never changeble in this
situation.

I hope this helps a little bit.

Cor
 
my english is bad (thats first) but problem ocures becouse :

WHAT HAPEND IF I HAVE TWO COMPUTERS (CLIENTS) AND THEY ADD NEW ROW TO DATABASE IN THE SAME TIME . in the start i will have same id key (before update) but access database will give second record one higher number (after update) ,

WHAT IF I USE THAT ID KEY FOR MAKING 'NAME' (as string witch i after save in the sam row as name od document) AND IN THAT OCUSION I WILL HAVE SAME 'NAME' OF DOCUMENTS (that not good i need unike 'name') ... THATS THE PROBLEM, I NEED TO GET DIFRENT ID KEY no mether is that update or not ,,... HOW

i know that is bad for access database logic to get new id key no mether if it's updated but , i don't figure out something bette

thanks all you for helping m

Voy
 
DONT USE CAPS

Voya said:
my english is bad (thats first) but problem ocures becouse :

WHAT HAPEND IF I HAVE TWO COMPUTERS (CLIENTS) AND THEY ADD NEW ROW TO
DATABASE IN THE SAME TIME . in the start i will have same id key (before
update) but access database will give second record one higher number (after
update) ,
See CONCURRENCY
WHAT IF I USE THAT ID KEY FOR MAKING 'NAME' (as string witch i after save
in the sam row as name od document) AND IN THAT OCUSION I WILL HAVE SAME
'NAME' OF DOCUMENTS (that not good i need unike 'name') ... THATS THE
PROBLEM, I NEED TO GET DIFRENT ID KEY no mether is that update or not ,,...
HOW ?
See Identity Seeds / Autoincrementing fields

i know that is bad for access database logic to get new id key no mether
if it's updated but , i don't figure out something better
 
Hi Voya,

I think that I would do something as this in your situation.

Dim ds As New DataSet
Database.GetDataset(ds)
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
Database.UpdateDataset(ds)
Database.GetDataset(ds)
Dim dv As New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv

Just as a sample of course,

Cor
 
Back
Top