ASP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingASP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 3rd, 2009, 06:53 AM
inyoman inyoman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 11 inyoman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 32 m 41 sec
Reputation Power: 0
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

		



%>

Reply With Quote
  #2  
Old November 3rd, 2009, 08:51 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 48th Plane (28500 - 28999 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 28,838 Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)  Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 3 Months 2 Weeks 1 Day 14 h 30 m 10 sec
Reputation Power: 2389
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"

Reply With Quote
  #3  
Old November 4th, 2009, 09:03 AM
inyoman inyoman is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 11 inyoman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 32 m 41 sec
Reputation Power: 0
Quote:
Originally Posted by Shadow Wizard
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"


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.

Reply With Quote
  #4  
Old November 4th, 2009, 09:58 AM
richyrich's Avatar
richyrich richyrich is offline
Contributing User
ASP Free Specialist (4000 - 4499 posts)
 
Join Date: Jun 2004
Location: Somewhere only we know...
Posts: 4,220 richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)richyrich User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 130723 Folding Title: Super Ultimate Folder - Level 1Folding Points: 130723 Folding Title: Super Ultimate Folder - Level 1Folding Points: 130723 Folding Title: Super Ultimate Folder - Level 1Folding Points: 130723 Folding Title: Super Ultimate Folder - Level 1Folding Points: 130723 Folding Title: Super Ultimate Folder - Level 1Folding Points: 130723 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 1 Week 3 Days 14 h 27 m 36 sec
Reputation Power: 1441
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...

Reply With Quote
  #5  
Old November 5th, 2009, 01:46 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 48th Plane (28500 - 28999 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 28,838 Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 21st Grade (Above 100000 Reputation Level)  Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2Folding Points: 549372 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 3 Months 2 Weeks 1 Day 14 h 30 m 10 sec
Reputation Power: 2389
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.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingASP Development > VBScript - General - Question - Is this a loop problem?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek