Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Generating sequential numbers with prefix

Generating sequential numbers with prefix

Generating sequential numbers with prefix


I have unique prefixes depending on what user is working on. When user needs a new number, I query database, find the last entry, remove the prefix and generate my next number in sequence. There are times when users are needing to generate multiple sequential numbers for a prefix. As long as they are the only ones requesting new numbers, all works well, it is when there are 2 or more needing a new number that I am having trouble. If while generating the multiple sequential numbers someone requests a number, it breaks the numeric sequence the first user is expecting to see.

This is where I am having trouble. I am in ASP.Net 4.0 using C#. To create the sequential numbers, I am using a for loop because the user will indicate how many they are needing. I suspect I am generating my sequential numbers the hard way, but I am not sure the best method.

So my question is, is there a way that once the for loop is generating the sequential list of numbers that another request from a different user has to wait until the first request is finished?

Example of the numbering:


If a user request a series of 25 numbers, I need that request to create say ZZ3333-000159 thru ZZ3333-000184 without one sneaking in there for a single request of ZZ4444-XXXXXXX in the middle somewhere.

below is not what the users are expecting.

Should be
etc unitl ZZ3333-000184

Hope this explains what I am trying to accomplish. No need to focus on the prefixes as there is specific criteria that is used to create those numbers, I just need to get the sequential numbers to workout.

Thank you for any suggestions.


RE: Generating sequential numbers with prefix

Are you storing the last generated number in a table? Seems the only way to correctly do it.

RE: Generating sequential numbers with prefix

Yes, once the number is created, I add the needed prefix and store it in a SQL server table.

RE: Generating sequential numbers with prefix

Honestly, this whole design seems a bit strange. Why do you need to have these prefixes? More information would be helpful to try to find a better solution.
And what exactly is the prefix the alpha portion or the numeric portion or is it the combination of both?
Also, what are number are you storing in your DB, is it just the numeric portion?

From what I see it looks like you are just storing a numeric value and then the alpha portion is like a userID or some sort of unique identifier is that correct?

I am not sure what your requirements are or why. But it seems to me that if you need sequential numbers, it should be per "UserID" (i.e. "ZZ3333", ZZ4444", etc.). Doing it this way will ensure that you will have sequential numbers. They way are you doing it now will almost certainly cause a skip in the sequence if more than one request for the last number is made at the same time.
If you want to continue the current way, it can become complicated. You will have to do something like do a rowlock or tablelock when someone queries the table, give them a sequence, and them immediately write to the table and unlock the row/table.

This might be a good time to sit down with some key people and make a smart business decisions as to why this is necessary and possibly change the requirements.

RE: Generating sequential numbers with prefix

Hi jbenson,

I do see how it is confusing. The prefix is based on products. So ZZ would be our widget product, XX would be product01, YY would be product02, etc. The 4 digits after the alpha is code for a process in the manufacturing of the product. The numbers after that have no specific meaning, just the next number on the list.

ZZ3333-111111 is a unique number for a process step for that product.

So each product has its own sequence of numbers that are after the prefix. When the user needs a new number, they select the product, I use that to find the last entry, trim to get the last 6 digits, add 1 and assign that number along with the prefix that all get stored for reference later. There are many times when a user is working a product and needs to have multiple numbers reserved/checked out for his process. Right now they get the numbers one at a time with no issue, but there is a desire to get multiples at one time.

This used to be done via an Excel spreadsheet, but as business grows this is not working as well as they want, and several times the Excel file has been getting corrupted and we lose some information.

Hope this helps explain what I am trying to solve.

Thanks for you time and help.

RE: Generating sequential numbers with prefix

Ok, so one thing you can do is store the product and sequence number in a table, if that is not what you are doing already.

Another way is to let the DB handle it. Again, I am not sure why this sequencing is important, or why it needs to be done, the DB has an identity column that can be used for this instead. But if this is what you need to do, so be it. Another thing is I am not sure why you need to reserve sequence numbers ahead of time.

What I would do is have a screen that allows the user to enter any number of rows on the screen that they need to. The user should not have to worry about grabbing sequence numbers. Now, when the user saves the rows, let's say you are using a gridview, pass the info to the db, and let the DB code (a stored procedure) handle adding a sequence number to the table for each row. This would help prevent gaps in the sequence, however, it is not fool proof.

RE: Generating sequential numbers with prefix

Hi jbenson,

That is an interesting solution, I had not thought of doing what you are explaining. I am going to give that a try as I think it would work.

Thank You

RE: Generating sequential numbers with prefix

Glad to help
Good luck and post back with your results / questions

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close