next ID value

  • Thread starter Thread starter Marco Martin
  • Start date Start date
M

Marco Martin

Hi,

I have a datatable in a dataset. In the table, I have a column "RecordID"
with AutoIncrement set to True. My table holds many records. How do I get
the next usable value for my "RecordID" from the datatable?

ie:
if the last record RecordID value is 23, how can I get that value?

Thanks in advance,

Marco
 
Marco,

You will have to select the max value of the id from the table and then
assign one to it.

However, this isn't too stable a scheme, and has many problems with it.
A better solution is to have a separate table with the last id in it, and
then bump up the value in the table whenever you need a new id.

Also, you might want to look into using a GUID for your identifier.
This way, you never have to worry about getting the "next" value. Each
value is always unique.

Hope this helps.
 
Thanks Nicholas,

I was hoping the dataset had a "Next Value" property.

regards,

Marco
Nicholas Paldino said:
Marco,

You will have to select the max value of the id from the table and then
assign one to it.

However, this isn't too stable a scheme, and has many problems with it.
A better solution is to have a separate table with the last id in it, and
then bump up the value in the table whenever you need a new id.

Also, you might want to look into using a GUID for your identifier.
This way, you never have to worry about getting the "next" value. Each
value is always unique.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marco Martin said:
Hi,

I have a datatable in a dataset. In the table, I have a column "RecordID"
with AutoIncrement set to True. My table holds many records. How do I get
the next usable value for my "RecordID" from the datatable?

ie:
if the last record RecordID value is 23, how can I get that value?

Thanks in advance,

Marco
 
Marco,
See
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconretrievingi
dentityorautonumbervalues.htm
in the VS.NET online help about retrieving Autonumber fields.
When you load your DataTable call FillSchema and then set AutoIncrement
to true, AutoIncrementSeed to -1 and AutoIncrementStep to -1 along with the
RowUpdatedEventHandler and things should work smoothly. Remember to update
all parent tables before any child tables that use these fields.
You may want to search through microsoft.public.dotnet. framework.adonet
for more information on this. Or see David Sceppa's book 'Microsoft ADO.NET
Core Reference' (highly reccommended).

Ron Allen
 
Marco Martin said:
I have a datatable in a dataset. In the table, I have a column "RecordID"
with AutoIncrement set to True. My table holds many records. How do I get
the next usable value for my "RecordID" from the datatable?

ie:
if the last record RecordID value is 23, how can I get that value?

To expand a bit on Nicholas's answer - don't forget that between you
fetching the data and you updating the database, other people could
well be adding more data. In other words, the next usable value at read
time may well not be the value which is automatically added when you
actually write your data back.
 
Back
Top