Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need Server side alternative to Session Variables - to track "next available number"

Posted on 2011-03-10
15
Medium Priority
?
303 Views
Last Modified: 2012-05-11
I've got a Purchase Requisition web form that pulls the top Requisition Number from the database;

TopReqID = reader("ReqID")

and puts the next available Req Number in a label on the page;

lblPurchaseRequisitionNumber.Text = TopReqID + 1
That number, TopReqID + 1, with then, of course be the next ReqID key value entered once the form is submitted.

The ReqID is the Identity Key in the Requisitions table.

*The problem is that while someone has the form open and displaying the "next available Req Number", which will be printed for signatures, someone else may open the form to submit a Purchase Req and it will grab the very same "next available Req Number" since the first one has not been submitted yet.

I need something like a Session Variable, but that the Server is aware of so that each time the form is opened it gets a different number.

Here is a scenario.  The last Req # in the DB is 29.  User A opens the form and Req # 30 is printed on the form.  Currently if User B opens the form before User A submits his, then User B will have Req # 30 also.  Not good.

What should happen is when User A opens the for he gets Req #30, then User B opens the form and gets Req # 31, User C opens his form at the same time as the others and gets Req #32.

User A eventually submits his form and record 30 is added to the table.
Users B and C abort without submitting the form at all.
Next day (whenever) any user opens the form, he gets #31

I do see the issue where User A submits #30, User B aborts and User C submits what he thinks is #32 but it goes into the DB as Identity record #31.  Dang.  Hmmm, also if User C submits his first then the record numbers will be out of order.  I guess I need a better solution.  Any ideas?

Thanks.
0
Comment
Question by:megnin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
15 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35098193
You could use a control table in your database that tracks the next requisition number, which you would store in your table separately from the actual ID of the record. Each time you retrieve the next number you increment it so that the next call will always be one further on.

That way you keep the Identity column as an auto-incrementing number that the user never sees, and you store a specific requisition number against the record.
0
 
LVL 59

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 35098249
above solution will create many gaps on your original table...

instead, dont show orderid until it is submitted to db
in db create a stored proc and in this sp, either run a query to get max+1 (or use identity and insert with null as id and get identity on this table to find the inserted id - i suggest this)
once you insert the record, you will have the orderid and you can return this to page, and show it to user...
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35098277
then instead of a stroed procedure directly use the autoincriment type for ID as suggested by carl
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 1

Author Comment

by:megnin
ID: 35098279
Yeah, that's just about what a colleague and I were just discussing.  The control table would use auto incrementing "Identity" values and the main order table would not.  The main table would have to accept whatever order number was submitted.

We also discussed making the form create a new order record when the form is opened and when it's submitted do an UPDATE to insert the field values.  The problem I see with that is you'd have an empty record every time someone just looked at the form then closed it unless you could DELETE that record if it wasn't "Submitted".  Is that possible (or practical)?
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35098309
that possible byt that will leave the gaps in ur ids
0
 
LVL 1

Author Comment

by:megnin
ID: 35098315
HainKurt, I think your solution is best as long as the order "form" can be printed by the user with the number on it because the paper copy must go for signatures.
0
 
LVL 1

Author Comment

by:megnin
ID: 35098341
Oh, yes, an ID is not reused when it's deleted, so deleting it would be pointless.
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35098359
autoincriment the field and show it to user after subbmision is the best possible solution in this case
0
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35098378
one more thing can be done...
you do as you have suggested i.e. insert a row the update it and delete in case user cancel the request.  but while getting the new id search for the gaps and assign the gap id to the new record
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 35098639
when you use autoincrement column in your table, there will be gaps only if the insert statement fails somehow... other than that there wont be any gaps... if gaps is not important us the one I suggested before...

if you DO NOT want any gaps, when you try to insert the record, find the min gap and use that one as new id for inserted record...

you can find the gap by

select min(id1)+1 as NewID from (
select t1.id id1, t2.id id2 from mytable t1 left join mytable t2 on t1.id+1=t2.id
) x
0
 
LVL 1

Author Comment

by:megnin
ID: 35099181
HainKurt, I'm so sorry.  I just looked throught all the comments and realized that you were the one who first suggested that solution.  Should I request Admin to do a point redistribution?
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 35100839
Yes please...
0
 
LVL 1

Author Comment

by:megnin
ID: 35108074
I submitted the request yesterday at 6:33 PM.  I haven't gotten a response yet, but it's not yet 8:00 AM.
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 35128591
This solution will work best for me.  Thank you very much.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question