Hi

How to generate the number in the text box when the database table is empty while form is loaded

regards
bhuvana

Dani AI

Generated

For (and following ’s request for details): the safest, simplest pattern is to let the database create the serial and read it back after the INSERT. That avoids race conditions when many users open the form at once. Typical flow: user edits -> on OK do the INSERT -> return the new id in the same operation -> display it. Database-specific return mechanisms include SQL Server’s OUTPUT or SCOPE_IDENTITY(), PostgreSQL’s RETURNING, and MySQL’s LAST_INSERT_ID().

If you must show a number when the form opens there are two common approaches, both with tradeoffs:

  • Reserve a sequence value up front (fast and concurrency-safe). Note that reserved values can create gaps if the user cancels.
  • Insert a lightweight "reserved" row and update it on save. This also creates gaps and requires cleanup logic for abandoned reservations.
    Do not use SELECT MAX(id)+1 — it is subject to race conditions. Also avoid holding a DB transaction open while the user edits (that can lock resources for too long).

Examples (conceptual):

SELECT NEXT VALUE FOR dbo.DocSeq AS ReservedDocEntry;
INSERT INTO Documents (...) OUTPUT inserted.DocEntry VALUES (...);
INSERT INTO documents (...) VALUES (...);
SELECT LAST_INSERT_ID();

See official docs for details and correct behavior: SQL Server SEQUENCE and OUTPUT/SCOPE_IDENTITY and PostgreSQL RETURNING and MySQL AUTO_INCREMENT / LAST_INSERT_ID.

Recommended Answers

All 2 Replies

What??

Post in the correct forum and explain in detail what you want and you may get the answer you seek :)

Hi

i want to generate the serial number( for example DocEntry 1,2,3 ....) in the textbox when the form open. after that it should be saved in the database while ok button is clicked. and then next number should be generated in the text box.

regards
bhuvana

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.