Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using windows application form in vb.netmy project is developed using 3tier arcitecture.in my project in sales invoice form i want the invoice code to be auto increment. I want to increment code value but its datatype is varchar.The id contain some prefix as alphabet e.g inv0000001.i am using sql server.the datatype varchar(10).pls some one hele me how to auto generate id.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Nov-11 19:11pm    
What have you done? How can you be sure 7 decimal digits would be enough?
--SA
Pete BSC 8-Nov-11 21:47pm    
If they are sure they will never produce more than 9,999,999 invoices, then 7 is enough.

Since this is in the VB.NET section, I'll give you my answer using code and a sequence table (you could do this within the database with triggers and/or computed columns and remove the need for a sequence table).

Create a table (or if you have a setting table) where you will read and update your next invoice sequence (1,2,3...9999999)

When you save a new invoice, fetch the sequence and increment your sequence table (if you fetch the sequence before you save you can run into concurrency issues if you have multiple users creating invoices). You may want to test before you save that your sequence is unique, if it is not, fetch another (until you have a unique sequence).

VB
'Fetch the sequence from your table
value = GetSequence()

'Test if unique invoice
While Not IsUniqueInvoice(value)
    value = GetSequence()
End While


GetSequence() would return the next invoice number from your sequence table:
User A: save invoice ... select sequence = 1, update sequence table = 2
User B: save invoice ... select sequence = 2, update sequence table = 3

Let say that User C and User D save at the same time where both users are assigned the same sequence:
User C: save invoice ... select sequence = 3, update sequence table = 4
User D: save invoice ... select sequence = 3, update sequence table = 4

User C's entry would save (since 3 is unique); however User D would fail (would create a duplicate invoice number). That is where the IsUniqueInvoice would prevent the duplication.

So you would want User D to fetch another sequence (loop until you have an unused sequence):
User D: select sequence = 4, update sequence table = 5

GetSequence() would fetch and update the sequence table. You could create a stored procedure that selects and updates the sequence in one call.
 
Share this answer
 
v2
Comments
Dave Kreskowiak 15-Oct-12 10:21am    
There's still a hole in this. It's possible, no matter how unlikely, for two clients to run this code at the same time, get the same sequence number, and both think that they have a unique number.

You won't know for sure that the ID is unique until the invoice record is inserted in the the table and see if SQL throws a constraint violation at you.
Pete BSC 24-Oct-12 9:50am    
Hi Dave, there is that chance; however,the call to IsUniqueInvoice could be called on save to test for duplication before saving. The question to ask is what kind of volume will new invoices be generated. I've used this method as we generate new "invoice numbers" in small intervals without issue. Worst case the DB throws the exception (with proper error handling) the user could re-save (with a new invoice number) their work without losing any data.

The ideal solution is let the database handle the sequencing: a) faster, b) if manually importing data the invoices would be numbered.
Please see Code Project article here:
[Flexible numbering sequences in T-SQL]
which contains a solution for such a problem.
Since on large-use applications there are many clients (or web servers), I use the database to generate the next sequence, with only 1 single call to the database.

Mike
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900