Need help to select and loop a table asp classic - asp-classic

I need some help in looping the right way. I'm missing something.
I have to select an order from a cart table and then mail a confirmation to the customer with the products and the sale company address from another table.
The problem is that the products in 1 order can come from 2 different companies so I need to first display the products from company 1 and then the address from company 1 and then the products from company 2 and then the address from company 2.
So it needs to look like this.
Product 1
Product 2
Company name 1
Address 1
Postcode 1
City 1
Product 3
Product 4
Company name 2
Address 2
Postcode 2
City 2
And this is what I have now.
sql = " SELECT * FROM cart WHERE ordernummer = "&ordernummer&" group by userId"
set rs = conn.Execute (sql)
do until rs.eof
uId=rs("userId")
sql7 = " SELECT * FROM cart WHERE userId = "&uId&""
set rs7 = conn.Execute (sql7)
do until rs7.eof
art=art&"<br>"&rs7("artikel")&" "&rs7("pris")&" kr"
rs7.movenext
loop
sql3 = " SELECT * FROM users WHERE userId = "&uId&""
set rs3 = conn.Execute (sql3)
foretagsnamn=rs3("foretagsnamn")
adress=rs3("adress")
postnr=rs3("postnr")
postadress=rs3("postadress")
email=rs3("email")
hamtatext=rs3("hamtatext")
kartlank=rs3("kartaemaillink")
companyinfo="<br><br>"&foretagsnamn&"<br>" &_
adress&"<br>" &_
postnr&"<br>" &_
postadress&"<br>" &_
"<a href=mailto:"&email&" style='text-decoration:none;color:#6d6d72;' target='_blank'>"&email&"</a><br>" &_
"<a href="&kartlank&" target='_blank' style='text-decoration:none;color:#F90;'>Se karta</a><br>" &_
"Du kan nu hämta dina varor: <span style='color:#F90;'>"& hamtatext &"</span><br><br>"
companyinfo2=companyinfo2+companyinfo
companyinfo=""
art2=art2+art
art = ""
rs.movenext
loop
message="<br>"&art2&companyinfo2
response.write message&"<br>"
And the db looks like this.
Where userId is the different companies.
But with this, it displays all products and then the company 1 info only.
I have tested with so many different variants of this, but I can't get it right.
So any input really appreciated, thanks.

Here is a sql-command that will give all the information in one record-set and then you can just write each product line, and when the userid changes, THEN you write the previous companies address (which you have in variables as before).
sql="select * from cart c join users u on c.userID=u.userID where c.ordernummer = "&ordernummer&" group by c.userId"
The sql above JOINs the two tables together ON the column userID in both tables. the c and u behind the table namnes are just shorter names for those tables. You could use the full names aswell.
THEN, in the do until loop, you write down the company address information as you've done, and then you do a check on the first row in the do until loop you check if the rs.userID is different than the variable userID you have saved, if so, write the address. Also you need to write the address of the last company outside the loop after there are no more rows :)
Full code (using as much of your code as I could):
sql = " select * from cart c join users u on c.userID=u.userID where c.ordernummer = "&ordernummer&" group by c.userId"
set rs = conn.Execute (sql)
uId=0
do until rs.eof
if uID<>rs("userId") and uID>0 then
'This next row is a new company, Write the company information here, before getting new info and writing new rows.
companyinfo="<br><br>"&foretagsnamn&"<br>" &_
adress&"<br>" &_
postnr&"<br>" &_
postadress&"<br>" &_
"<a href=mailto:"&email&" style='text-decoration:none;color:#6d6d72;' target='_blank'>"&email&"</a><br>" &_
"<a href="&kartlank&" target='_blank' style='text-decoration:none;color:#F90;'>Se karta</a><br>" &_
"Du kan nu hämta dina varor: <span style='color:#F90;'>"& hamtatext &"</span><br><br>"
response.write companyinfo&"<br />"
end if
uId=rs("userId")
foretagsnamn=rs("foretagsnamn")
adress=rs("adress")
postnr=rs("postnr")
postadress=rs("postadress")
email=rs("email")
hamtatext=rs("hamtatext")
kartlank=rs("kartaemaillink")
'Do a response.write of this data here for each row below.
art=art&"<br>"&rs("artikel")&" "&rs("pris")&" kr"
response.write art&"<br />"
art = ""
rs.movenext
loop
'Remeber to write the last company data outside of the loop aswell:
companyinfo="<br><br>"&foretagsnamn&"<br>" &_
adress&"<br>" &_
postnr&"<br>" &_
postadress&"<br>" &_
"<a href=mailto:"&email&" style='text-decoration:none;color:#6d6d72;' target='_blank'>"&email&"</a><br>" &_
"<a href="&kartlank&" target='_blank' style='text-decoration:none;color:#F90;'>Se karta</a><br>" &_
"Du kan nu hämta dina varor: <span style='color:#F90;'>"& hamtatext &"</span><br><br>"
response.write companyinfo&"<br />"
Hope it helps.

Related

How to get 3 different Class Element to display in single memo field in table row order

I am trying to pull data from a web page within the comment section. There are three comment section labeled by ClassName of "odd", "Comment" and "null" separated by . What I am trying to do is populate the data into a text field in my form but to keep the order in tact. Also a bonus would be to add a space or tab to separate each .
I am able to export to three different fields but I would rather have everything populated into a single comment field on my form.
HMTL Code
<tr id="4178695" class="null">
<td>Date: 03/20/2017 14:37</td>
<td>Tech: Wood, Ken</td>
<td>Process Order. <br/>-Direct-00:15-Inquiry<br/>-Indirect-00:15-Paperwork</td>
</tr>
<tr id="4011079" class="odd">
<td>Date: 12/19/2016 14:45</td>
<td>Tech: Pine, Al</td>
<td>Quarterly Contact: Left VM for and sent f/u email.<br/>-Direct-00:15-Phone</td>
</tr>
<tr id="3394536" class="Comment">
<td>Date: 09/29/2016 12:09</td>
<td>Tech: Fosgate, Rockford</td>
<td>9/28/2016: Sent email to family providing new contact number. <br/>-Direct-00:15-Other</td>
</tr>
<tr id="4178695" class="null">
<td>Date: 08/20/2016 14:37</td>
<td>Tech: Wood, Ken</td>
<td>Process Order. <br/>-Direct-00:15-Inquiry<br/>-Indirect-00:15-Paperwork</td>
</tr>
<tr id="3394536" class="Comment">
<td>Date: 07/29/2016 12:09</td>
<td>Tech: Fosgate, Rockford</td>
<td>9/28/2016: Sent email to family providing new contact number. <br/>-Direct-00:15-Other</td>
</tr>
VBA Code
Dim t, u, v As String
Set oColl = doc.getElementsByClassName("odd")
Set oColl2 = doc.getElementsByClassName("Comment")
Set oColl3 = doc.getElementsByClassName("null")
For Each o In oColl
t = t & "Odd: " & o.innerText & vbCrLf & vbCrLf
Next
For Each p In oColl2
u = u & "Comment: " & p.innerText & vbCrLf & vbCrLf
Next
For Each q In oColl3
v = v & "Null: " & q.innerText & vbCrLf & vbCrLf
Next
Screen.ActiveForm.MessageBox = "Most Recent Comments: " & vbCrLf & t & vbCrLf & u & vbCrLf & v
The code above works but in spits out each comment by cycling through each ClassName label.
You can select all TR elements of the TABLE with comments:
Set table = doc.getElementById("tableid")
or
Set table = doc.getElementsByTagName("TABLE")[0].
Then iterate all rows (tr) by using table.children, optionally filtering by class name (className property).
Update
Here is a more complete example:
Set doc = IE.Document
Set table = doc.getElementById("commentTable").getElementsByTagName("tr")
For Each row In table
Set cells = row.getElementsByTagName("td")
For Each cell in cells
r = r & cell.innerText & vbTab
Next cell
r = r & vbCrLf & vbCrLf
Next row
I was able to successful pull the data with the following code:
Dim table As Object, htmlcomment As Object
Dim r As String
Set doc = IE.Document
Set table = doc.getElementById("commentTable").getElementsByTagName("tr")
For Each htmlcomment In table
r = r & htmlcomment.innerText & vbCrLf & vbCrLf
Next
Screen.ActiveForm.MessageBox = "Most Recent Comments: " & vbCrLf & r
Now, I just have to figure out how to add a space or tab to separate the <TD>.

Search on ASP Classic

I have this problem on my search page . For example on my search page I have this data on a table
01234567
24567801
on my search if i type 01, that 2 record appear what i want is for the
01234567 to only appear. How can i get this result?
this is the code I have on my search page:
dim search
search = ""
if tsearch <> "" then
if len(trim(tsearch)) <> 0 then
search= " AND (NUMBER_LiSt LIKE '%" & tsearch & "%')"
end if
end if
#Vogel612 is correct that the LIKE is looking for any instance of '01' in the NUMBER_LIST items, as you have the wildcard symbols (%) at the beginning and end of the LIKE.
If you are looking only for numbers that start with tsearch (eg: '01') then remove the '%' at the start of the tsearch:
search= " AND (NUMBER_LiSt LIKE '" & tsearch & "%')"
Similarly, to look for only numbers that end in tsearch, add the starting, and remove the trailing '%':
search= " AND (NUMBER_LiSt LIKE '%" & tsearch & "')"
Other than LIKE there are other SQL Commands for searching (like MSSQL's PATINDEX) but they are more tricky that a good LIKE.

Update Query using two tables asp.net vb

Please help me with this update query. I COMPLETELY understand how redundant this is but I NEED it.
Upon users creating a User account which only contains the ID, Username, Password and CustomerID I must also have a record in Customers create itself but null everything and the User.User_Customer_ID must update to the newly created Customer.Customer_ID
I have everything working but this pesky update query. The ONLY thing it needs to do is change User.User_Customer_ID to Customer.Customer_ID I managed to story the User.User_ID in an integer called UserID and Customer.Customer_ID in an integer called CustomerID I have tried a million different ways with no success
Here is what one of the many queries I have tried.
1. UPDATE [User]
SET User_Customer_ID = Customer.Customer_ID
FROM Customer
WHERE [User].User_ID = '16'
AND Customer.CUstomer_ID = '19'
( ? would be the integer UserID and CustomerID )
Here is how it looks before it is inserted into the SQL command in VB
UpdateUserCustomerID = "UPDATE [User] SET User_Customer_ID = Customer.Customer_ID FROM Customer WHERE ([User].User_ID ='" & UserID & "' AND Customer.Customer_ID = '" & CustomerID & "')"
Access Database / ASP.Net / VB
Table:
Customer:
Customer_ID
Customer_First_Name
Customer_Last_Name
Customer_Street_Address
Customer_City
Customer_State
Customer_Zip
Customer_Email
User:
User_ID
User_UserName
User_Password
User_Customer_ID
It seems pretty unlikely to me that the IDs are text fields (columns), if they are number fields, the quotes are one of the the problems.
As far as I can see, this:
"UPDATE [User] SET User_Customer_ID = Customer.Customer_ID FROM Customer WHERE ([User].User_ID ='" & UserID & "' AND Customer.Customer_ID = '" & CustomerID & "')"
Should be:
"UPDATE [User] SET User_Customer_ID = " & CustomerID _
& " WHERE ([User].User_ID = " & UserID

Multiple Tables Linked By ID

I have to write an ASP page that has connection to 1 database and then queries two tables one which has the header detail in and then the second which has the order lines in, each table has a ORDER_NUMNER.
These Tables contain a sales orders which I need to print out into an HTML page any help on this would be great as ASP is not my main language.
In general:
Instantiate and open your database connection: (see www.connectionstrings.com for more information)
dim conn
conn.open "your connection string goes here"
Open a recordset for the master table and detail table:
dim rst
rst.open "select * from tblMaster left join tblDetail on tblMaster.ORDER_NUMBER = tblDetail.ORDER_NUMBER where ORDER_NUMBER = 4",conn,1,3
Exit out of there are not records
if rst.eof then
rst.close
conn.close
Response.end
end if
Print header info (for fields order_date, order_number, and order_company:
response.write "Company: " & rst.fields("order_company") & "<br>"
response.write "Date: " & rst.fields("order_date") & "<br>"
response.write "Order Number: " & rst.fields("order_number") & "<br>"
Loop through records, reading all records from detail table: (for fields item_desc, item_qty, item_cost)
while not rst.eof
response.write "Item: " & rst.fields("item_desc") & "<br>"
response.write "Qty: " & rst.fields("item_qty") & "<br>"
response.write "Cost: " & rst.fields("item_cost") & "<br>"
rst.MoveNext
wend
Close the recordset
rst.close
Close the connection
conn.close
<%
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Mode = 3
objConn.Open "driver definition and connection string"
SQLStrJ = "SELECT * FROM table1 t1 JOIN table2 t2 ON t1.ORDER_NUMBER=t2.ORDERNUMBER;"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open SQLStrJ, objConn, 1, 3
Do Until objRS.EOF = True %>
<html><%=objRs("field")%></html>
<%
objRs.MoveNext
Loop
%>
You'll see in the loop an example of dropping field data into HTML. You will probably want to put the connection and recordset definition in an includable function that you can just pass a SQL string into since you'll be using it a lot. And don't forget to close the connection when you're done. Good luck.. classic ASP is a bit of a mess.

Classic ASP: Execute 2 Update Statements in single function

I am writing Classic ASP program.In one function, I have to use 2 update statements to one table in one function. First Statement is update the quantity of invoice and second update statement is base on that update Purchase Order quantity and Purchase Requisition quantity, I need to update one flag field. Can I write in same function as following:
SET RS = app.Execute("SELECT PRInvoiceNo, Quantity FROM PurchaseOrderDetails WHERE CoID='" & param & "'")
do while RS.EOF=false
app.Execute("UPDATE PurchaseRequisitionDetails SET PO_Quantity = PO_Quantity + " & RS("Quantity") & " WHERE CoID='" & param & "' AND PRInvoiceNo = '" & RS("PRInvoiceNo") & "'")
app.Execute("UPDATE PurchaseRequisitionDetails SET FullyPaidFlag=CASE WHEN PO_Quantity >= Quantity THEN 1 ELSE 0 END WHERE CoID='" & param & "' AND PRInvoiceNo = '" & RS("PRInvoiceNo") & "'")
RS.MoveNext
loop
The problem is in the loop the first statement is properly worked. Second one not work. What can it be? Can I do like this or not?
Well, I have to go, but be sure to check the following:
Response.Write(RS.RecordCount) -- are there any records? Or, do a Response.Write("hello") inside the loop to make sure.
Check that RS("Quantity"), param, etc are not null. If they are, your string concatenation will result in a null string.
Also, please, please don't forget to escape your variables!
Replace(param, "'", "''")
Good night!

Resources