Change response to only respond one set of values - asp-classic

I am very novice at asp. Here is my code:
response.write("<script>")
dim counter: counter = 0
do until rs.EOF
for each x in rs.Fields
counter = counter + 1
response.write "var CC" & counter & "=" & x.value & ";"
next
rs.MoveNext
loop
response.write("</script>")
This currently returns both columns from my DB:
<script>var CC1=ALFKI;var CC2=13579;var CC3=ALFKI;var CC4=246;</script>
I only want the second column to return, which is the numbers (13579 & 246).
What should I change?

Not sure I should be answering this, but to loop and only write out on the even iteration use Mod() to test the remainder.
response.write("<script>")
dim counter: counter = 0
do until rs.EOF
for each x in rs.Fields
counter = counter + 1
'Use Mod() to check counter is even
If counter Mod 2 = 0 Then response.write "var CC" & counter & "=" & x.value & ";"
next
rs.MoveNext
loop
response.write("</script>")
Useful links
The Magical Mod Function

Ad an IF statement, based on your counter.
for each x in rs.Fields
counter = counter + 1
if(counter = 1) then
response.write "var CC" & counter & "=" & x.value & ";"
end if
next

Try this
response.write("<script>")
dim counter: counter = 0
do until rs.EOF
for each x in rs.Fields
counter = counter + 1
if IsNumeric(x.value) then
response.write "var CC" & counter & "=" & x.value & ";"
end if
next
rs.MoveNext
loop
response.write("</script>")
The trick is to check "If IsNumeric" then do what you want.

Related

Insert looped variable to one table column

I have Checkbox form and I am trying to save my loops values into a variable.
This is how I am looping:
For i = 1 to Request.Form("packages").count
Set packnamn = ObjConn.Execute ("Select * from services_package where id='"&
Request.Form("packages")(i) &"'")
panamn = packnamn("name") & "-"
Response.write panamn
Next
This will output
Greece-English-Spanish-
Now im trying to save the whole output in my MySQL. But i'm only getting the last value of the loop (Spanish-), This is my insert code.
Dim sql1
sql1= "insert into tariff_plan(package)"
sql1=sql1 & " VALUES "
sql1=sql1 & "('" & panamn &"')"
on error resume next
ObjConn.Execute sql1,recaffected
if err<>0 then
Response.Write(err.description)
else
response.write "Saved"
End if
You have to add the values to a variable where you are doing the response.write and do the insert after the loop.
panamn = ""
For i = 1 to Request.Form("packages").count
Set packnamn = ObjConn.Execute ("Select * from services_package where id='"&
Request.Form("packages")(i) &"'")
panamn = panamn & packnamn("name") & "-"
Next
Response.write panamn
'Do the insert here

How to reuse an array in asp.net vb

I'm having to build a table of web pages and languages, i.e. page 1: en it de in a schema where there could be up to 14 languages. The page contents are held in a database. So to build the table I'm doing the following:
Dim rowArrayList As New ArrayList
Dim thisRow(languageNum) As String 'languageNum equates to number of columns -1
Database access then:
'# Create row array of cell arrays
If pageName <> lastPageName Then
lastPageName = pageName
If j >= languageNum Then
rowArrayList.Add(thisRow)
Array.Clear(thisRow, 0, thisRow.Length)
j = 0
End If
thisRow(0) = "<td class=""pageName"">" & pageName & "</td>"
End If
'# Iterate each cell in the row
For i As Integer = 1 To languageNum - 1
If thisRow(i) = "" Then
If transReady = False And active = False Then
thisRow(i) = "<td class=""trans"">" & langISO & "</td>"
ElseIf transReady = True And active = False Then
thisRow(i) = "<td class=""notActive"">" & langISO & "</td>"
ElseIf transReady = True And active = True And i = thisLangID Then
thisRow(i) = "<td class=""active"">" & langISO & "</td>"
End If
End If
j = j + 1
Next
The build the table:
'# Build output table
For Each row As String() In rowArrayList
tableBody.Text += "<tr>"
For Each cell As String In row
If cell = "" Then
tableBody.Text += "<td class=""notTrans""> </td>"
Else
tableBody.Text += cell
End If
Next
tableBody.Text += "</tr>"
Next
The table displays beautifully BUT every row contains the data for what should be the last row. How can it be fixed it so each thisRow is unique in the the rowArrayList? At the moment, every time thisRow is added to rowArrayList, every rowArrayList index is overwritten, not just the one being added.
For the quick fix, instead of this:
Array.Clear(thisRow, 0, thisRow.Length)
Do this:
thisRow = New String(languageNum) {}
or this:
ReDim thisRow(languageNum)
However, I suspect there are some simple design choices you could change that would drastically change this code for the better.

Show/Split 4 rows in a table in Loop

The code below is how to show/split 2 rows in a table.
But how do I get it to show/split it to 4 rows and not only two ???
dim bCloseRow
bCloseRow = True
Do while not objTrucks.eof
If bCloseRow Then response.write "<tr>"
bCloseRow = Not bCloseRow
response.write "<td><input type=checkbox name=dno value=" & objTrucks("TRUCK_NO") & objTrucks("TRUCK_NO") & ">" & objTrucks("TRUCK_NO") & "</td>"
If bCloseRow Then response.write "</tr>"
objTrucks.movenext
loop
Thanks for any help.
If there are more rows then four in the recordset you have to make a row more. And if there is less then four you have to insert some empty td tags.
Hope this helps
const iCols=4
dim iCount:iCount=0
if not objTrucks.eof then
response.write "<table>"
Do while not objTrucks.eof
if iCount=0 then response.write "<tr>"
response.write "<td><input type=checkbox name=dno value=" & objTrucks("TRUCK_NO") & objTrucks("TRUCK_NO") & ">" & objTrucks("TRUCK_NO") & "</td>"
objTrucks.movenext
iCount = iCount + 1
if iCount=iCols then
response.write "</tr>"
iCount=0
end if
loop
if iCount>0 then
for i = iCount to iCols-1
response.write "<td></td>"
next
response.write "</tr>"
end if
response.write "</table>"
end if
I would do this using a counter instead of a Boolean flag:
Const numCols = 4
Dim counter
counter = 0
Do While Not objTrucks.EOF
If counter Mod numCols = 0 Then
Response.Write "<tr>"
End If
counter = counter + 1
Response.Write "<td><input type=checkbox name=dno value=" & objTrucks("TRUCK_NO") & objTrucks("TRUCK_NO") & ">" & objTrucks("TRUCK_NO") & "</td>"
If counter Mod numCols = 0 Then
Response.Write "</tr>"
End If
objTrucks.MoveNext
Loop

Classic ASP sub procedure call in Response.Write

I have a classic ASP question.
Attempting to do this: the recordset is a simple list of years from 1995 to 2020; and i am trying to make 2010(current year) the default selection in the drop down.
issue: I trying to call a Sub proc in "Response.Write", but it keeps giving me this error:
"Error '800a000d' Type mismatch: 'selectyear' "
Below is the code, the Attempt 1 works with out any problem. But when i move that "if" logic to a sub procedure and call it in the Request.Write, it gives me the error.
Can any one please explain why Attempt1 works and Attempt2 wouldnt.
' Attempt 1:
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">"
dim selyr
while not rsYEAR.EOF
if CINT(rsYEAR.fields("year")) = year(now) then
selyr = "selected"
else selyr = ""
end if
Response.Write"<option value='" & rsYEAR.fields("year") & "' "& selyr &" >" & cstr(rsYEAR.Fields("year"))
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
' Attempt 2:
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td> <td> <select name='theyear' style=""WIDTH: 67px"">"
dim selyr2
while not rsYEAR.EOF
Response.Write "<option value='" & rsYEAR.fields("year") & "' " & cstr(selectyear(cint(rsYEAR.fields("year")))) &" >" & cstr(rsYEAR.Fields("year"))
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
'close and clean up
rsYEAR.Close
set rsYEAR = nothing
I would greatly appreciate your response.
thank you,
Shiva
I am guessing that cint(rsYEAR.fields("year")) is throwing the error because there is data that cannot be converted to int. I would expect that to happen in both cases though.
You shouldn't need the cstr in cstr(selectyear(cint(rsYEAR.fields("year")))) in the second attempt, as I assume selectyear is already returning a string. Can you show the code for selectyear?
(How has this sat for so long without a correct answer?)
A Sub does not have a value, so you can't Response.Write it. You need to either use a function, or put the Sub call on its own line.
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">"
dim y
while not rsYEAR.EOF
y = rsYEAR.fields("year")
Response.Write "<option value='" & y & "'" & IsCurr(y) & ">" & y & "</option>"
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
Function IsCurr(yr)
if Cstr(yr) = Cstr(year(now)) then
IsCurr = " selected"
else
IsCurr = ""
end if
End Function
Using a sub instead of a function, this would become
rsYEAR.Open qYEAR, objconn, 0, 1
response.Write "<tr><td>Year:</td><td><select name='theyear' style=""width: 67px"">"
dim y
while not rsYEAR.EOF
y = rsYEAR.fields("year")
Response.Write "<option value='" & y & "'"
IsCurr y
Response.Write ">" & y & "</option>"
rsYEAR.MoveNext
wend
response.Write "</select></td></tr>"
rsYEAR.Close
Sub IsCurr(yr)
if Cstr(yr) = Cstr(year(now)) then
Response.Write " selected"
end if
End Sub

asp pagination problem

Hi i have a problem with this asp pagination. it seems to be putting all the links in the one row, so i think it might have something to do with the check of the int i...
but im not that familar with asp. can anyone shed any light on this problem.
the folders contain pdfs for each day of the month, named A08P2.pdf A09P2.pdf etc...
Thanks
i = 1
Set fc = f.Files
Set ff = f.SubFolders
For Each f1 in fc
intPage = cint(mid(f1.name,2,2))
chrEdition = mid(f1.name,1,1)
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
end if
i = i + 1
Next
You should move the incrementing of the i (i=i+1) inside the if...end if, since if i is 9 and you encounter two chrEditions that are not 'A' then i will become 11 and will never match the closing condition i=10:
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
i = i + 1
end if

Resources