Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get next autoincrement value for datatable column?

Tags:

.net

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?

like image 650
Rado Avatar asked Feb 20 '26 11:02

Rado


2 Answers

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.

like image 130
Yugal Jindle Avatar answered Feb 22 '26 00:02

Yugal Jindle


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.

like image 36
hometoast Avatar answered Feb 22 '26 01:02

hometoast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!