I have a datatable with an AutoIncrement column. How can I get the next increment (-1,-2,-3,...) for the column without adding a new row?
I got the solution to the problem (Not a work around) :
To get the next auto-increment value from SQLServer :
SELECT IDENT_CURRENT('table_name'); -- This will fetch the present auto-increment value.
Therefore,
SELECT IDENT_CURRENT('table_name')+1; -- Next auto-increment value.
This will not break if rows are deleted from a table with identity and key is not consecutive.
Hope that helps.
If it fits in to your design, you can always create the new row (datatable.newrow) and it will already have assigned the nextId. It's now wasted but the row isn't committed to the table until you AcceptChanges.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With