Link to home
Create AccountLog in
Avatar of mokule
mokuleFlag for Poland

asked on

Identity field problem

Here is my problem
In form load i've got
   this.ogiSelectEventBindingSource.AddNew();

In form close I've got something like this

   this.ogiSelectEventBindingSource.EndEdit();
   this.ogiSelectEventTableAdapter.Update(this.ogierDataSet.ogiSelectEvent);
   DataRowView serv = (DataRowView)this.ogiSelectEventBindingSource.Current;
   ogiSelectEventRow ser = (ogiSelectEventRow)serv.Row;
   int id = ser.evtIdent;

evtIdent is autoincrement field in a database table.
Let's imagine I've got records with evtIdent 1,2,3.
There were also records with evtIdent 4,5, but they were delete
After initial AddNew()
ser.evtIdent has value 4
After Update() the record is save to a table in database with evtIdent value 6
But ser.evtIdent stays 4.

What should I do to force ser.evtIdent to follow what is really saved in a table?
Avatar of nsanga
nsanga

you said...

"There were also records with evtIdent 4,5, but they were deleted"

are they deleted from data base......what database you are using.....

I think, data base doesn't reuse the numbers even if the rows are deleted and so new row has got 6. But in you dataset/datatable the event id is given a new value 4.

why do you need a autonumber, you can do this way

1)remove auto number property from data base and make eventid just a number
2) once you read the data base into your datatable, then you set the eventid as autonumber.
3) while update to database, the values in data table will only go to data base.


I would presume the DB engine is keeping the highest previously assigned value and using that for the autoincrement.  

If it didn't do it that way, every delete would require recalculation of that field on every delete.
Avatar of mokule

ASKER

I'm using MS SQL Server.
I don't need diagnose of how database is working. I know it.
I'd like to know how to get newly added autoincrement field value.
i already explained above


1)remove auto number property from data base and make eventid field just a number in database.
2) read data from database
3) once you read the data base into your datatable, then you set the eventid as autonumber in your datatable.
4) just try by this
Avatar of mokule

ASKER

@nsanga
Should i really take it seriously?
If so explain me please how it would work if two users do this operation at the same time?
can you post some of your relevant code  where you read database and others...



Avatar of mokule

ASKER

I've posted most my relevant code
Maybe only this
            this.ogiSelectEventTableAdapter.Fill(this.ogierDataSet.ogiSelectEvent, 1 , null);

The other is autocreated in a huge DataSet strongly typed and such a things.

One thing i've just noticed is that from some unknown reason the autocreated evtIdent field in tabbleadapter has no Unique property set.
Similar such a fields from other tables has it set.
Maybe something goes wrong in tableadapter autocreation. But where to check I don't know.

this is what i was saying.....


remove auto number property from data base and make eventid field just a number in database.


you said

"One thing i've just noticed is that from some unknown reason the autocreated evtIdent field in tabbleadapter has no Unique property set."

there I am saying set "AutoIncrement" for eventid field in tabbleadapter.


this has a effect of
database not having "Auto number" for eventid but in the program tabbleadapter's eventid field has "AutoIncrement" property.

This will make eventid field in your program to be written to database.











Avatar of mokule

ASKER

1. No i won't remove autoincrement feature from the database field.
Otherwise it stops working completely.

2. evtIdent IS saved to database
The problem is that saved value is different that this hold in tableadapter.
I'm searching a way to keep this values synchronized.

3. I've managed to have Unique property set for evtIdent field while autocreating Tableadapter.
However this doesn't solve a problem with evtIdent not reflecting the real value saved in database table.
in that case may be you can re query your data after calling update......
Avatar of mokule

ASKER

that's what I'm looking for, but how to do it?

call this again.......

this.ogiSelectEventTableAdapter.Fill(this.ogierDataSet.ogiSelectEvent, 1 , null);
Avatar of mokule

ASKER

You're joking all the time :)
I'm not in such a good mood.
How do I know which is my just new added row?
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account