This is a discussion on "Generating ID or Session Problem? Options" within the Classic ASP section. This forum, and the thread "Generating ID or Session Problem? Options are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Generating ID or Session Problem? Options
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
Generating ID or Session Problem? Options
Hi everyone, i'm currently developing an online shopping cart, the order number is automatically generated.
Once i started to shop it's working fine. Then if i tried to shop using other pc, i encountered wierd thing, wherein the order number is being started from 1 again. e.g. orderNo: 60727001 then, once the transaction is done, it will be incremented by 1. But when i try to shop again using other computer, orderNo will be set to 60727001 again. Below is my code in Generating OrderNo: ============================== theYear = right(year(now()), 2) theMonth = month(now()) theDay = day(now()) if theMonth < 10 then theMonth = "0" & theMonth end if if theday < 10 then theDay = "0" & theDay end if theDate = theYear & theMonth & theDay Sub CreateNewOrder() Application.lock if Application("orderID") = "" then sql = "SELECT MAX (orderID) AS theLast FROM orders WHERE Left(orderID, 6) = '" & theDate & "'" set rs = Server.CreateObject("ADODB.RecordSet") rs.Open sql, ConString , adLockReadonly if NOT isNull(rs("theLast")) then theLast = cInt(right(rs("theLast"), 3)) + 1 if theLast > 999 then 'ala lang else do while len(theLast) < 3 theLast = "0" & theLast Loop end if else theLast = "001" end if theLast = theDate & theLast Application("orderID") = theLast end if intOrderID = Application("orderID") Session("orderID") = intOrderID strSQL = "SELECT * FROM orders WHERE NULL" Set rs = Server.CreateObject("ADODB.Recordset") rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic, adCmdText rs.AddNew rs.Fields("status").Value = "OPEN" rs.Fields("orderID") = intOrderID rs.Update Application("orderID") = Application("orderID") + 1 Application.Unlock rs.Close Set rs = Nothing End Sub Sub AddToOrder(nOrderID, nProductID, nQuant) strSQL2 = "SELECT * FROM itemsOrdered" Set rs2 = Server.CreateObject("ADODB.Recordset") rs2.Open strSQL2, Conn, adOpenKeyset, adLockOptimistic, adCmdText rs2.AddNew rs2.Fields("orderID").Value = nOrderID rs2.Fields("productID").Value = nProductID rs2.Fields("quantity").Value = nQuant rs2.Update rs2.Close Set rs2 = Nothing End Sub 'Main program intProdID = Request.form("intProdID") intQuant = Request.form("intQuant") intOrderID = CStr(Session("orderID")) if intOrderID = "" then CreateNewOrder end if sqlText = "SELECT * FROM itemsOrdered WHERE orderID =" & intOrderID & "AND productID =" & intProdID Set rsOrder = Server.CreateObject("ADODB.Recordset") rsOrder.Open sqlText, Conn if rsOrder.EOF then txtInfo = "This item has been added to your cart." AddToOrder intOrderID, intProdID, intQuant else txtInfo = "This item is already in your cart." end if %> ================================= OR does global.asa file should do anything about this? Please see below: ============================= ' ***** global.asa **** <script LANGUAGE=VBScript RUNAT=Server> Sub Application_OnStart Dim vPath, pPath, ConString vPath = "database\Prod.mdb" pPath = Server.MapPath( vPath ) ConString = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & pPath & ";" & "JET OLEDB Application("visits") = 0 Application("Active") = 0 set Conn = Server.CreateObject("ADODB.Connection") Conn.Open ConString dim theMonth, theYear, theDate dim rs theYear = right(year(now()), 2) theMonth = month(now()) theDay = day(now()) if theMonth < 10 then theMonth = "0" & theMonth end if if theday < 10 then theDay = "0" & theDay end if theDate = theYear & theMonth & theDay sql = "SELECT MAX (orderID) AS theLast FROM orders WHERE Left(orderID, 6) = '" & theDate & "'" set rsMaxOrder = Server.CreateObject("ADODB.RecordSet") rsMaxOrder.Open sql, ConString , adLockReadonly if NOT isNull(rsMaxOrder("theLast")) then theLast = cInt(right(rsMaxOrder("theLast"), 3)) + 1 if theLast > 999 then 'what do you do here? else do while len(theLast) < 3 theLast = "0" & theLast Loop end if else theLast = "001" end if theLast = theDate & theLast if rsMaxOrder.EOF then Application("orderID") = theLast else Application("orderID") = rsMaxOrder("orderID") + 1 end if rsMaxOrder.Close set rsMaxOrder = Nothing Conn.Close set Conn = Nothing End Sub Sub Session_OnStart Session("Start") = Now Application.lock Application("visits") = Application("visits") + 1 intTotal_visitors = Application("visits") Application.unlock Session("VisitorID") = intTotal_visitors Application.lock Application("Active") = Application("Active") + 1 Application.unlock End Sub Sub Session_OnEnd Application.lock Application("Active") = Application("Active") - 1 Application.unlock End Sub </SCRIPT> ============================== please check why this thing happens? thanks. |
|
|
|
#2
|
|||
|
|||
|
Re: Generating ID or Session Problem? Options
Using the application variable probably isn't the best way to do this but I didn't see a specific problem glancing over your code. But it's kind of difficult to look over it on here when it doesn't indent the code and format it.
Typically I'd leave the order number generation up to the database and use either a primary key or a per user order number....e.g. user ID 15 order number two could be 000001500002. This is slightly safer than what you have as you could run into concurrency problems and potentially get two orders with the same number. You seem to be doing a lot of coding simply to get a unique ID. My guess would be that this line is not evaluating correctly : |
|
#3
|
|||
|
|||
|
Re: Generating ID or Session Problem? Options
I would def. leave this job up to the database. My companies e-commerce system uses (in simple terms) a table which represents your order header, which has your system generated order number, which is also the basket header (baskets and orders are all the same, it's just basket is one of the orders status', as is paid & dispatched)
|
![]() |
| Tags |
| generating, session, problem, options |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PHP Session *not your usual* problem | ysamu | PHP Forum | 0 | Mar 28th, 2008 10:00 |
| [SOLVED] Session data problem | longstand | PHP Forum | 15 | Dec 9th, 2007 18:28 |
| Session Timeout problem | LameesAyman | ASP.NET Forum | 2 | May 2nd, 2007 14:45 |
| session problem | fender79 | Classic ASP | 8 | May 18th, 2005 02:26 |
| problem with Session variable | andrew_perez5 | Classic ASP | 0 | Jan 24th, 2005 03:42 |