Welcome to Dream.In.Code
Become an Expert!

Join 150,413 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 982 people online right now. Registration is fast and FREE... Join Now!




Ensuring Unique QuoteIDs

 
Reply to this topicStart new topic

Ensuring Unique QuoteIDs, mysql, asp.net, c#

Footsie
29 Mar, 2008 - 12:51 AM
Post #1

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 308



Thanked: 4 times
Dream Kudos: 50
My Contributions
Using ASP.net I'm trying to set up a basic online shop. I've set up a database in MySql that has a variety of tables in it.
(Customers, Suppliers, Products etc)
I've set up a table called Quotes in order to keep track of "online" quotes like this:
CODE

QUOTE TABLE:
--------------
PK_QuoteID
FK_CustomerID
TotalPrice
QuoteDateTime
...


As people browse through products and prices they "build" their own quote by selecting a few items.
They will then click a button marked "Get Quote" which will take them to the Quote page.

This page I want to structure like a conventional quote, and in order to get a Quote number, I'm running a select statement like this:
CODE
SELECT last_insert_id() FROM quotes


EDIT:
Then I add 1 in the ExecuteScalar statement to get the next Quote number:
CODE

   //scalar returns an object, so convert to int and add 1 for new quote
            int newQuote = Convert.ToInt32(comQuoteNo.ExecuteScalar()) + 1;


There will be another button here to "Accept Quote" which will write all details to the "Quote" table and have an option for the user to Print.

This works fine at the moment, but I can forsee the following problem:

If 2 or more users click the "Get Quote" button at around the same time the select statement will return the same quote number for all of them. This will cause issues with non-unique QuoteIDs.

Do I need to lock the number somehow?
The problem with doing this is that if the 2nd user rejects his quote there is an unused quote number in the DB.

Is there a problem with my DB structure?

Any thoughts would be appreciated.

This post has been edited by Footsie: 29 Mar, 2008 - 05:04 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Ensuring Unique QuoteIDs
29 Mar, 2008 - 06:30 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Why not save yourself a lot of trouble and just use the AUTO_INCREMENT Attribute on your ID field. It will the automatically increment the last ID by 1, ensuring unique ID's
User is offlineProfile CardPM
+Quote Post

Footsie
RE: Ensuring Unique QuoteIDs
29 Mar, 2008 - 08:18 AM
Post #3

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 308



Thanked: 4 times
Dream Kudos: 50
My Contributions
My QuoteID is set to auto increment, and as the primary key.
But the values only get written to the table at the end when the user clicks "Accept Quote".

The issue is with the sequence:
CODE

1> User clicks Get Quote
2> Select statement executes to get last insert + 1
     (which gives new quote number) and displays it
3> Only then will the user click Accept / Decline Quote  
4> If Accepted, Values are written to DB table and the
      primary key increments correctly


Now, if another user or 3 execute point 1> before the first user clicks "Accept", and writes the values to the DB in point 4> The same quote number will be displayed.

I hope that explains it better.

This post has been edited by Footsie: 29 Mar, 2008 - 08:19 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Ensuring Unique QuoteIDs
29 Mar, 2008 - 09:41 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Why are you making this so difficult on yourself (unless this is a homework assignment, and if it is then your instructor is making this harder than it should be). What you do is before entering the quote information check to be sure the same quote ID isn't already present, if it is then increment the current ID by 1 then insert.

That will cover your problem, as 2 people cannot insert data at the same exact time (which is highly unlikely as the timestamp of the insertion is calculated including milliseconds, which makes it virtually impossible to 2 records to be inserted at the same exact time).


So:

  • Get the last ID before giving the quote
  • If the user selects "Save Quote":
    • Check to database to see if the ID this user has is already there
    • If it is
      • Get the current ID and increment by 1
      • Insert the quote data
    • If it doesn't already exist insert the data

User is offlineProfile CardPM
+Quote Post

Footsie
RE: Ensuring Unique QuoteIDs
29 Mar, 2008 - 10:06 AM
Post #5

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 308



Thanked: 4 times
Dream Kudos: 50
My Contributions
Hey Psycho.
No, not a homework assignment. I wish it was - it would mean I am still at school / college and starting this stuff a lot younger!
Just me being hard on myself.
I don't know why I didn't think of checking twice - that should most definitely solve my almost non-existent problem.

And Psycho, thanks again for the help.
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Ensuring Unique QuoteIDs
29 Mar, 2008 - 10:32 AM
Post #6

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,299



Thanked: 19 times
Dream Kudos: 725
My Contributions
I've been told(although I can't guarantee) that running LAST_INSERT_ID() will return the last inserted ID for that connection - so in your case, if two people ran LAST_INSERT_ID() at the same time, they would each get their specific last inserted ID's. You may want to test it to be sure, but that's what I've been told.
User is online!Profile CardPM
+Quote Post

Trogdor
RE: Ensuring Unique QuoteIDs
29 Mar, 2008 - 01:53 PM
Post #7

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
you can use a kludge: have a separate table with one autoincrement field, where you just insert a new record on the start, that gives you the unique id.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month