1

I have a Form Windows program in C# that adds a record to the database and can remove it.

In the database i have ID (which is Auto Number), but if i delete a record and if i want to add another record instead, the Auto Number increases and doesn't add the missing numbers.

I mean that if i have 9 records in my Access Database and i want to remove a record, it will be 8, but when i add a new record, i get 10 instead of 9. like this picture:

enter image description here

Is there any solution for that?

2
  • 2
    The answer is NO. when using auto-incrementing, you have to accept the way it works. Otherwise, you have to implement your own auto-incrementing. Commented Oct 6, 2013 at 17:15
  • BTW, the reason for why it works that way is to get the highest performance. If it worked the way you want, we need a much more complicated algorithm to find the next identity instead of just increasing the last used value. Commented Oct 6, 2013 at 17:23

3 Answers 3

1

If it's an auto number, the database will generate a number greater than the last one used - this is how relational databases are supposed to work. Why would there be solution for this? Imagine deleting 5, what would you want to do then, have the auto number create the next record as 5? If you are displaying an id in your C# app - bad idea - then change this to some other value that you can control as you wish.

However what you are trying to achieve does not make sense.

Sign up to request clarification or add additional context in comments.

Comments

1

if i delete a record and if i want to add another record instead, the Auto Number increases and doesn't add the missing numbers.

[...]

Is there any solution for that?

The short answer is "No". Once used, AutoNumber values are typically never re-used, even if the deleted record had the largest AutoNumber value in the table. This is due (at least in in part) to the fact that the Jet/Ace database engine has to be able to manage AutoNumber values in a multi-user environment.

(One exception to the above rule is if the Access database is compacted then the next available AutoNumber value for a table with a sequential AutoNumber field is reset to Max(current_value)+1.)

For more details on how AutoNumber fields work, see my other answer here.

Comments

1

In MS access, there is no any solutions for this. But in case of sql server you can create your own function rather using Identity column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.