PDA

View Full Version : guaranteed to be unique -generated number?


[o_O]
01-13-2003, 07:12 AM
For my little cart app, I needed a way of coming up with orderNumber for each (*fake)customer. I used vb script in my global.asa to generate a random number but there is no way to make sure it hasn't generated it before without a query to the DB.

I have been looking into the idea of using an ID field but haven't came up with a performance friendly and simple way regarding that so i ask:

Is it possible to have asp to come up with a random number that is actually unique?

raf
01-13-2003, 07:31 AM
well, i don't think so.
unless of coarse, you trust your app, your server/host, power supplier etc etc.
i remember a similar post some months ago, where another programmer,also used an application variable to assign the random numbers. this goes well until the application is restarted ...

to me, the only safe way is inserting the new order/client/... into a database with an autonumber variable + retrieving it and storing it in a session-variable or dragging it along (using hidden fields or via the querystring or something like that.
never checked it, but i doen't think it takes up that much resources. (since this is vital information, you need to make small sacrifices)

bostjank
01-13-2003, 08:04 AM
Do you really need a number?
Otherwise you can generate a unique ID using current date and time.

Bostjan

[o_O]
01-13-2003, 08:38 AM
inserting the new order/client/... into a database with an autonumber variable + retrieving it and storing it in a session-variable


O.k looks like this is what i'll have to do. Could you give a little detail on how you personally would do it exactly?

I will use session variables. The cart is currently 1 table:

cart
order_no <-!
order_item
order_date
order_quantity

So the orderNo is the unique part -the part that each user has his or her own assigned to them. But f course using a DB i am thinking the process of assing is something like

-user clicks add to cart
-check for existing session("orderNO")
-if none is found insert a new record in some table?
'temporderNo' or something
-then pull that record (primary key ID) and plasce it into the session variable.
-finally add the item to cart.

am i on the right track here :confused:

raf
01-13-2003, 11:00 AM
The best way to go, depends on a lot of things. (How important are the in basket orders, how customized is your shopping cart, what sort a database are you using, how many simultaneous connections, etc etc.)
I usually try to customize my apps a bit. So customers need to register and login. I don’t use cookies and only very limited javascript.
I store the ‘in basket’ information always in the querystring, hidden fields or in session-variables.
This I how I normally do it:
-user logs in. I store the info I need (userID, security profile, layoutoption) in session variables.
-orders (in basked) are stored in querystring or session-variables or in hidden fields
-when the order is confirmed, I insert new records (1 for each product) in a table with at least the userID and productsID and the quantity (it’s probably best to include an autonumber variable in this table)

If the in-basket info is important to you, or, when you want to allow your users to save the in-basket orders so that they can confirm them later, then I’d insert the record after adding the product to the basket, and include a ‘status’ for that record (inbasket, confirmed, paid, delivered…) and an autonumber-variable (like prodorderID). After inserting the record, you select the prodorderID and drag it along in session-variable/querystring/hiddenfield. When the order in confirmed, you just update the status of these records (or delete the inbasket-records, after a specified amount of time)

How you select this prodorderID, depends on the database you’re using. I usually work with an sql-statement like this (because it’s fairly bulletproof and universal)

Dim inserttime
Inserttime=Timer ‘timer=number of seconds since midnight
Sql=”INSERT INTO orderdetail (userID, prodID, timer) VALUES (theID, secondID, thetimer)
Sql=replace(sql,”theID”,session(userID))
Sql=replace(sql,”secondID”,request.form(prodID))
Sql=replace(sql,”thetimer”,inserttimer)

Execute and check number inserted

Sql=”select Max(prodorderID) from orderdetail where userID=theid and timer=thetimer”
Sql=replace(sql,”theID”,session(userID))
Sql=replace(sql,”thetimer”,inserttimer)


So basically, I just retrieve the last inserted record from that user, by using his id and the insertiontime as conditions. You can use the combination of each field you inserted as condtion. But again, for some databases, you have better sollutions

Off coarse this is just my opinion. I think that there were other threads about his sort a thing, so it’s probably not a bad idea to run a search.

whammy
01-14-2003, 12:39 AM
If you're using a database, you should use a primary key... no question there. That way you KNOW it's ALWAYS unique. :)