In VB .NET how do we get the new row ID number in an access database table BEFORE inserting the new row?
-
I'm programming with VB .NET 2010 and using a Microsoft Access Database file which is bound to my project as a Dataset. Now I want to know: BEFORE inserting a new row into my dataset table what will be the new ID number, or totally is it possible to know? Surely you know the autonumber ID of a table is different with row count number, for there may be deleted rows between the table rows. Like the following example: Row 0: ID=1 Row 1: ID=2 Row 2: ID=4 (ID number 3 is deleted) Row 3: ID=5 Row 4: ID=9 (ID numbers 6,7,8 are deleted) Now how do we know what ID number of Row 5 will be before inserting the new row? Perhaps ID numbers 10 and 11 are either deleted or not, so it may be 10, may be 11 or 12 or whatever! Is there a way to know it BEFORE adding or inserting the new row into the table? Thank you beforehand.
-
I'm programming with VB .NET 2010 and using a Microsoft Access Database file which is bound to my project as a Dataset. Now I want to know: BEFORE inserting a new row into my dataset table what will be the new ID number, or totally is it possible to know? Surely you know the autonumber ID of a table is different with row count number, for there may be deleted rows between the table rows. Like the following example: Row 0: ID=1 Row 1: ID=2 Row 2: ID=4 (ID number 3 is deleted) Row 3: ID=5 Row 4: ID=9 (ID numbers 6,7,8 are deleted) Now how do we know what ID number of Row 5 will be before inserting the new row? Perhaps ID numbers 10 and 11 are either deleted or not, so it may be 10, may be 11 or 12 or whatever! Is there a way to know it BEFORE adding or inserting the new row into the table? Thank you beforehand.
There is no way to know beforehand if you are using a standard identity value. If you need to know this beforehand, you are either going to have to generate the value yourself, or use something like a GUID instead - which you can assign in your code.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
There is no way to know beforehand if you are using a standard identity value. If you need to know this beforehand, you are either going to have to generate the value yourself, or use something like a GUID instead - which you can assign in your code.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
I'm programming with VB .NET 2010 and using a Microsoft Access Database file which is bound to my project as a Dataset. Now I want to know: BEFORE inserting a new row into my dataset table what will be the new ID number, or totally is it possible to know? Surely you know the autonumber ID of a table is different with row count number, for there may be deleted rows between the table rows. Like the following example: Row 0: ID=1 Row 1: ID=2 Row 2: ID=4 (ID number 3 is deleted) Row 3: ID=5 Row 4: ID=9 (ID numbers 6,7,8 are deleted) Now how do we know what ID number of Row 5 will be before inserting the new row? Perhaps ID numbers 10 and 11 are either deleted or not, so it may be 10, may be 11 or 12 or whatever! Is there a way to know it BEFORE adding or inserting the new row into the table? Thank you beforehand.
One way is to store the last ID number in another table and increment it whenever a row is inserted into the table.
-
One way is to store the last ID number in another table and increment it whenever a row is inserted into the table.