|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
VBScript - General - Question - Is this a loop problem?
Please someone help me with this loop problem in the code below. I dont know whether it is a loop problem or not, but i get error that say The Script take too long to run. Please help me, i am really stuck
Code:
<!-- #INCLUDE file="myDatabase.inc" -->
<%
MaxPrice=Request.Form("MaxPrice")
userID = Session("userID")
itemID = request("IID")
set conn = server.CreateObject ("ADODB.Connection")
conn.Open "Driver=" & dbDriver & "; Server=" & dbServer & "; Port=" & dbPort & "; Database=" & dbDatabase & "; Uid=" & dbUser & ";Pwd=" & dbPassword & ";"
set rs = Server.CreateObject("ADODB.Recordset")
'To get current time
sql="SELECT NOW()"
rs.open sql,conn
myTime = rs(0)
rs.close
'Get Asking Price and Increment
sql="SELECT MinPrice, increment FROM myItem WHERE IID = "& itemID &""
set rs = Server.CreateObject("ADODB.Recordset")
rs.open sql,conn
price = rs(0)
increments = rs(1)
rs.close
increments = ((increments/100) * price)
price = 0
'To Get the Current Highest Price
sql="SELECT Max(WinPrice) as MaxPrice FROM myBid WHERE IID = "& itemID &""
set rs = Server.CreateObject("ADODB.Recordset")
rs.open sql,conn
currentPrice = rs(0)
rs.close
'If user not logged in
IF userID = 0 THEN
Response.write "Please Login First"
Else
'To check the place bid value
IF (currentPrice > MaxPrice) OR (MaxPrice = "") then
Response.write "Please Enter Your Maximum Bid value"
else
'call function
callFunction = Bid(itemID,userID,price,MaxPrice)
Function Bid(myItemID, myUserID, Price, MaxPrice)
set conn = server.CreateObject ("ADODB.Connection")
conn.Open "Driver=" & dbDriver & "; Server=" & dbServer & "; Port=" & dbPort & "; Database=" & dbDatabase & "; Uid=" & dbUser & ";Pwd=" & dbPassword & ";"
set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT BID FROM myBid WHERE IID = "& myItemID &" AND UID = "& myUserID &""
rs.open sql,conn
'if the user has never bid yet for the item then create his information in myBid
if rs.eof then
rs.close
set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM myBid",conn, 3, 3
rs.AddNew
rs("IID")=myItemID
rs("UID")=myUserID
rs("WinPrice")=price
rs("MaxPrice")=MaxPrice
rs("Time")=myTime
rs.update
rs.close
'this is if the user already bid before note: second function will run to this
else
rs.close
set rs = Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM myBid WHERE IID = "& myItemID &" AND UID = "& myUserID &"",conn, 3, 3
rs("WinPrice")=price
rs.update
rs.close
end if
call ResolveBid(ItemID)
End Function
End If
End If
'second function
Function ResolveBid(ItemID)
'this is to be used in loop
isResolved = True
set conn1 = server.CreateObject ("ADODB.Connection")
conn1.Open "Driver=" & dbDriver & "; Server=" & dbServer & "; Port=" & dbPort & "; Database=" & dbDatabase & "; Uid=" & dbUser & ";Pwd=" & dbPassword & ";"
set rs1 = Server.CreateObject("ADODB.Recordset")
'Get the current Highest Winning Price from myBid
sql="SELECT MAX(WinPrice) as WinPrice FROM myBid WHERE IID = "& ItemID &""
rs1.open sql,conn1
currentHighest = rs1(0)
rs1.close
'Get the increment
sql="SELECT increment From myItem WHERE IID = "& ItemID &""
set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.open sql,conn1
increment = rs1(0)
rs1.close
increment = ((increment/100) * currentHighest)
totalprice = (currenthighest + increment)
'Get the user who maxprice is high enough
sql="SELECT * FROM myBid WHERE IID = "& itemID &""
set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.open sql,conn1
'if field not finished
if not rs1.eof then
'do until finish
do until rs1.eof
'if the user Max price is higher than current highest + calculated increment then
if (rs1("MaxPrice") > totalprice) AND (rs1("WinPrice") <> currentHighest) THEN
'set currenthighest into currenthighest + the calculated increment
currentHighest1 = currentHighest + increment
'call the first function to update the WinPrice with the currentHighest
call Bid(itemID, rs1("UID"), currentHighest1, "")
'set isResolved to false, this will cause the next if command to loop over
isResolved = False
end if
rs1.MoveNext
'since isResolved was set to False, and rs1.eof then it will
'move the cursor to first row thow it to the first IF statement
'only until the first IF statement is never true, then this will stop
if not isResolved AND rs1.eof then
rs1.MoveFirst
isResolved = True
end if
loop
end if
rs1.close
set rs1=nothing
set conn1=nothing
set rs=nothing
set conn=nothing
Response.Write "Succesfully Updated"
End Function
%>
|
|
#2
|
||||
|
||||
|
if myBid is big table (i.e. contains millions of records) it explains the problem.
your current code is taking all the data from this table. change all occurrences of this: Code:
"SELECT * FROM myBid" to this instead: Code:
"SELECT * FROM myBid WHERE 1=0" |
|
#3
|
|||
|
|||
|
Quote:
thx for the reply. no, the table is not that big. contain only less than 10 record when i am testing it. i am so stuck im gonna die ![]() however, the weird thing is that eventho it says the script take too long to run, the record is still updated. |
|
#4
|
||||
|
||||
|
I'm not sure I fully understand the logic of your loop and if statements, but why not just alter your SQL query to include the (rs1("MaxPrice") > totalprice) AND (rs1("WinPrice") <> currentHighest) condition, as it only does something to records that match this criteria.
Therefore, these are the only records your query should return. I would guess the speed is down to you going back to the first record if isResolved is false and you're at eof. Without being able to understand the logic though, it's a bit difficult to comment.
__________________
Policy Check I'd rather have a full bottle in front of me, than a full frontal lobotomy...
|
|
#5
|
||||
|
||||
|
hmm... on closer look I found possible infinite loop in your code.
change your loop to be this and let us know how it goes: Code:
Dim safetyCounter
safetyCounter = 0
Do Until rs1.EOF
safetyCounter = safetyCounter + 1
If safetyCounter>999 Then
Response.Write("too many iterations!<br />")
Exit Do
End If
'if the user Max price is higher than current highest + calculated increment then
If (rs1("MaxPrice") > totalprice) AND (rs1("WinPrice") <> currentHighest) Then
'set currenthighest into currenthighest + the calculated increment
currentHighest1 = currentHighest + increment
'call the first function to update the WinPrice with the currentHighest
Call Bid(itemID, rs1("UID"), currentHighest1, "")
'set isResolved to false, this will cause the next if command to loop over
isResolved = False
End If
rs1.MoveNext
'since isResolved was set to False, and rs1.eof then it will
'move the cursor to first row thow it to the first IF statement
'only until the first IF statement is never true, then this will stop
If (isResolved=False) And (rs1.EOF) Then
rs1.MoveFirst
isResolved = True
End If
Loop
this will put limit of 999 iterations, and break from the loop if there are more. |
![]() |
| Viewing: ASP Free Forums > Programming > ASP Development > VBScript - General - Question - Is this a loop problem? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|