Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 6th, 2006, 12:49 PM
Authorized User
 
Join Date: Mar 2006
Location: , Ohio, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Is looping the answer?

Hi all. I have an order page with multiple items. Each item has an itemnumber and a qty that I want to insert into the corresponding columns in a SQL database table. Each quantity value is selected from a dropdown. I want to add only the items where the user has selected a quantity from the dropdown box and ignore the rest. Is there a way to loop through the values, associate them as the proper itemnumber/qty pair and insert them into the database? I would prefer not to have to use itemnumber1, itemnumber2, qty1, qty2, etc. as database fields. The variables in the page can have those names, but I'd like to pass them to the database using simply idnumber and qty.

I've been experimenting with various loop structures, but can''t get the results I'm looking for. Any help is appreciated.

Reply With Quote
  #2 (permalink)  
Old March 6th, 2006, 08:13 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you post your code?

A couple links of interest:

    Add a new record to a database: http://www.planet-source-code.com/vb...4&txtCodeId=23
    VBScript Looping Statements: http://www.w3schools.com/vbscript/vbscript_looping.asp
    VBScript Tutorial: http://www.w3schools.com/vbscript/default.asp


- A.Kahtava
Reply With Quote
  #3 (permalink)  
Old March 7th, 2006, 01:05 AM
Authorized User
 
Join Date: Mar 2006
Location: , Ohio, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, the "itemqty" and "menuitemid" fields are not being added to the database. I'm using a stored procedure to insert the records. The values show up if I "itemqty(X)" and "menuitemid(X)" in a Response.Write statement, but how do I get the values into my database using the stored procedure?


Here's the code:
<%
Function qString(str)
tempStr = Replace(str, "<BR>", "")
bString = Replace(tempStr, CHR(13), "<BR>")
qString = Replace(Replace(bString, "'", "''"), CHR(34), """)
End Function

Dim objConn

eventid = Session("eventid")

qty0 = Request.Form("qty0")
qty1 = Request.Form("qty1")
qty2 = Request.Form("qty2")
qty3 = Request.Form("qty3")
qty4 = Request.Form("qty4")
qty5 = Request.Form("qty5")
qty6 = Request.Form("qty6")
qty7 = Request.Form("qty7")
qty8 = Request.Form("qty8")
qty9 = Request.Form("qty9")
qty10 = Request.Form("qty10")
qty11 = Request.Form("qty11")
qty12 = Request.Form("qty12")
qty13 = Request.Form("qty13")

menuitemid0 = Request.Form("menuitemid0")
menuitemid1 = Request.Form("menuitemid1")
menuitemid2 = Request.Form("menuitemid2")
menuitemid3 = Request.Form("menuitemid3")
menuitemid4 = Request.Form("menuitemid4")
menuitemid5 = Request.Form("menuitemid5")
menuitemid6 = Request.Form("menuitemid6")
menuitemid7 = Request.Form("menuitemid7")
menuitemid8 = Request.Form("menuitemid8")
menuitemid9 = Request.Form("menuitemid9")
menuitemid10 = Request.Form("menuitemid10")
menuitemid11 = Request.Form("menuitemid11")
menuitemid12 = Request.Form("menuitemid12")
menuitemid13 = Request.Form("menuitemid13")

sID = Session.SessionID
ordernumber = sID

Dim qty(14)
qty(0) = qty0
qty(1) = qty1
qty(2) = qty2
qty(3) = qty3
qty(4) = qty4
qty(5) = qty5
qty(6) = qty6
qty(7) = qty7
qty(8) = qty8
qty(9) = qty9
qty(10) = qty10
qty(11) = qty11
qty(12) = qty12
qty(13) = qty13

Dim itemid(14)
itemid(0) = menuitemid0
itemid(1) = menuitemid1
itemid(2) = menuitemid2
itemid(3) = menuitemid3
itemid(4) = menuitemid4
itemid(5) = menuitemid5
itemid(6) = menuitemid6
itemid(7) = menuitemid7
itemid(8) = menuitemid8
itemid(9) = menuitemid9
itemid(10) = menuitemid10
itemid(11) = menuitemid11
itemid(12) = menuitemid12
itemid(13) = menuitemid13

adCmdTxt = 1

strUpdate = "insert_OrderItems_1 '" & ordernumber & "', '" & itemqty & "', '" & menuitemid & "', '" & sID & "', '" & eventid & "';"

set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Application("dinners_DSN_ConnectionString")

For x = 0 to 13
If qty(X) > "0" Then
menuitemid = itemid(X)
itemqty = qty(X)
set objCmd = Server.CreateObject("ADODB.Command")
set objCmd.ActiveConnection = objConn
objCmd.CommandText = strUpdate
objCmd.CommandType = adCmdTxt
objCmd.Execute
End if
Next

Set objCmd = Nothing
objConn.Close
Set objConn = Nothing
Response.Redirect "tables-dbtd.asp"
%>

<understatement> I'm not the most efficient coder.</understatement> But I'm always willing to learn new ways to do things. As always, any help is greatly appreciated.
Reply With Quote
  #4 (permalink)  
Old March 7th, 2006, 09:08 AM
Authorized User
 
Join Date: Mar 2006
Location: , Ohio, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's the stored procedure:

CREATE PROCEDURE [insert_OrderItems_1]
    (@ordernumber_1 [bigint],
     @itemqty_2 [smallint],
     @menuitemid_3 [bigint],
     @sID_4 [bigint],
     @eventid_5 [bigint])

AS INSERT INTO [dinners_dbtd].[dinners_sqladmin].[OrderItems]
     ([ordernumber],
     [itemqty],
     [menuitemid],
     [sID],
     [eventid])

VALUES
    (@ordernumber_1,
     @itemqty_2,
     @menuitemid_3,
     @sID_4,
     @eventid_5)
GO

Reply With Quote
  #5 (permalink)  
Old March 7th, 2006, 09:16 AM
Authorized User
 
Join Date: Mar 2006
Location: , Ohio, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I should also add that I get the right values if I test with:

For x = 0 to 13
If qty(X) > "0" Then
menuitemid = itemid(X)
itemqty = qty(X)
Response.Write menuitemid &itemqty
End if
Next

But they aren't picked up in the insert statement.


Reply With Quote
  #6 (permalink)  
Old March 7th, 2006, 10:20 AM
Authorized User
 
Join Date: Mar 2006
Location: , Ohio, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, fixed it. Changed this:

strUpdate = "insert_OrderItems_1 '" & ordernumber & "', '" & itemqty & "', '" & menuitemid & "', '" & sID & "', '" & eventid & "';"

set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Application("dinners_DSN_ConnectionString")

For x = 0 to 13
If qty(X) > "0" Then
menuitemid = itemid(X)
itemqty = qty(X)
set objCmd = Server.CreateObject("ADODB.Command")
set objCmd.ActiveConnection = objConn
objCmd.CommandText = strUpdate
objCmd.CommandType = adCmdTxt
objCmd.Execute
End if
Next

to this:


set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Application("dinners_DSN_ConnectionString")

For x = 0 to 13
If qty(X) > "0" Then
menuitemid = itemid(X)
itemqty = qty(X)
strUpdate = "insert_OrderItems_1 '" & ordernumber & "', '" & itemqty & "', '" & menuitemid & "', '" & sID & "', '" & eventid & "';"
set objCmd = Server.CreateObject("ADODB.Command")
set objCmd.ActiveConnection = objConn
objCmd.CommandText = strUpdate
objCmd.CommandType = adCmdTxt
objCmd.Execute
End if
Next

This is why I shouldn't stay up so late.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Try this, answer me surendran PHP How-To 1 November 14th, 2006 07:09 AM
Please answer canasdaq XML 3 April 19th, 2006 09:04 AM
i want answer titto_oo7 C# 7 February 9th, 2006 03:59 PM
answer IvAnR C++ Programming 1 July 22nd, 2005 08:01 AM
About the answer!!! soso ASP.NET 1.0 and 1.1 Basics 0 July 9th, 2003 11:05 PM



All times are GMT -4. The time now is 02:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.