Illegal Syntax. Expected valid start name character - vbscript

Not sure whats gone wrong here, not very good at this stuff so apologise in advance. Ive got the below but faulting when trying to run. Not sure whats gone wrong with this part as used to work before on an older server and migrated the data across.
<%
rs.Close
if request.form () <> "" then
varSearch = replace(request.form("T1"),"*","%")
varSearch = replace(varSearch," ","%")
sql = "SELECT * FROM [Cards] WHERE [CardTypeID] = 'C' AND [Name] LIKE '%" & varSearch & "%'
ORDER BY [Name]"
rs.Source = sql
rs.Open
%>
Thanks.

request.form() - you're not capturing anything
<%
rs.Close
if request.form ("T1") <> "" then
varSearch = replace(request.form("T1"),"*","%")
varSearch = replace(varSearch," ","%")
sql = "SELECT * FROM [Cards] WHERE [CardTypeID] = 'C' AND [Name] LIKE '%" & varSearch & "%'
ORDER BY [Name]"
rs.Source = sql
rs.Open
%>

Related

ASP Classic Code Logic using If statements to check inputs from a form

I have this code in an ASP page written 20+ years ago. I am trying to update the code an I am having trouble figuring this out:
If Request("SUTyp").Count > 1 THEN
CountCriteria = 0
For intMulti=1 to Request("SUTyp").Count
If Request("SUTyp")(intMulti) <> "*" Then
CountCriteria = CountCriteria + 1
If CountCriteria = 1 Then
SUTypCode = "((tblSU.SUTypCode) LIKE '" & Request("SUTyp")(intMulti) & "')"
Else
SUTypCode = SUTypCode & " OR ((tblSU.SUTypCode) LIKE '" & Request("SUTyp")(intMulti) & "')"
End If
Else
SUTypCode = ""
intMulti = Request("SUTyp").Count
End If
SUTyp is a variable that is coming from a form on the previous page. There is an option (from that previous page) in the select box on the form to 'Select All' or to 'Select Multiple Options'.
<Select name="SUTyp" Size="7" Multiple >
<OPTION VALUE="*" SELECTED>all study unit types
<%
do while (not rsSUType.eof) and (SaveError <> -2147467259)
if rsSUType.Fields("SUTypCode").Value = "*" then
%>
<OPTION VALUE="<%response.write(rsSUType.Fields("SUTypCode").Value)%>" SELECTED>.
<%response.write(rsSUType.Fields("SUTypCode").Value)%>,
<%response.write(rsSUType.Fields("SUTyp").Value)%>
<%
Else
%>
<OPTION VALUE="<%response.write(rsSUType.Fields("SUTypCode").Value)%>">.
<%response.write(rsSUType.Fields("SUTypCode").Value)%> -
<%response.write(rsSUType.Fields("SUTyp").Value)%>
<%
End If
rsSUType.movenext
loop
%>
</Select>
It is then using some data to create a variable (SUTypCode =) for a WHERE clause to query the database. What I don't know is the logic of what it is saying. Specifically:
For intMulti=1 to Request("SUTyp").Count
If Request("SUTyp")(intMulti) <> "*" Then
CountCriteria = CountCriteria + 1
If CountCriteria = 1 Then
SUTypCode = "((tblSU.SUTypCode) LIKE '" & Request("SUTyp")(intMulti) & "')"
I am guessing that somehow the ASP form sets some kind of variable intMulti and uses that for a comparison.
If someone could shed some light on this and so I can re-write it that would be great. This is being created using PHP, so I am just trying to figure out what this means so I can create the equivalent.
Thanks!

Query returns only 4 out of 12 values of my table. Why is this happening?

I have created a script which I am using on 2 different system. One is Windows 10 with SQL Server Express 2019 and the other is on Windows 7 (32bit) with SQL Server Express 2014. The db I am using has the same structure. The code I am using in ASP Classic is exactly the same on both machines! But in the win7 machine does not work as it should!
I am having a query to my db and the problem is that SOME of the values I am getting are empty. The code I am using is the following:
<%
Set rs21 = Server.CreateObject("ADODB.Recordset")
strSQL21 = "SELECT * FROM otherfiles WHERE animalid LIKE '" & lngRecordNo & "'
AND historyid LIKE '" & historyid & "' order by datedone DESC"
rs21.Open strSQL21, adoCon
%>
BLAH BLAH BLAH
<%
Do While not rs21.EOF
historyid = rs21("historyid")
if not historyid = "" then
Set rshis = Server.CreateObject("ADODB.Recordset")
strSQLhis = "SELECT * FROM history WHERE id_no LIKE '" & historyid & "' order by id_no DESC"
rshis.Open strSQLhis, adoCon
'------------------- if I remove the following line (Set rscol) it works fine ---------------------------
Set rscol = Server.CreateObject("ADODB.Recordset")
strcol = "SELECT * FROM hospitals WHERE id_no = "& rsGuestbook21("hosid") &" order by id_no DESC"
rscol.Open strcol, adocon
color = rscol("color")
%>
BLAH BLAH BLAH
<%=rs21("datedone")%> ---> empty value
<%=rs21("id_no")%> ---> works fine!!!
I am 100% sure that my table has its values. In fact as I mention above only but setting the rscol = Server.CreateObject("ADODB.Recordset") then the values becomes ??empty??. Commenting this line works fine.
I just figured out my ??mistake??...
Not really sure if it is a mistake thou... but the problem solved when I placed a field that had a DATA TYPE of "nvarchar(MAX)" at the end of my of my table! Weird stuff??? Do you think that there is something more than that in which I should investigate?

Valid response causes "Subcript out of range"

I've got a classic ASP application that contacts a database and receives a valid response but then crashes with
Error Number 9: Subscript out of range
after exiting the IF block the db call is made in. What's odd is that the same code is currently working in production. As far as I can tell they're configured identically (but I suspect there's a subtle difference that's causing this issue) and have identical code bases.
What I want to know is:
Where is this array that I'm supposedly attempting to reach a non-existent index of? I don't see it and the error gives no line number. Is there a chance something is not working correctly in the adodb library?
Perhaps this is a common problem having to do with a certain patch and my particular db connection library? Have you had a similar experience?
How do I troubleshoot a problem that doesn't immediately present itself? Should I just start putting troubleshooting statements in the library?
Explanation of what's happening in the code: When the cookie "click" is received err.number is 0. When the cookie "bang" is received the err.number is 9. It then crashes with that error at the end of the IF block.
<%#Language="VBSCRIPT"%>
<% Server.ScriptTimeout = 150 %>
<%
On Error resume Next
%>
<!--#include file="adovbs.inc"-->
<!--#INCLUDE FILE="DBConn.asp"-->
<!--#INCLUDE FILE="ErrorHandler.asp"-->
<%
'Application Timeout Warning
sessionTimeout = 20
advanceWarning = 5
jsTimeout = (sessionTimeout - advanceWarning) * 60000
'If the users session has expired
If Session("USERNAME") = "" or Session("USERNAME") = NULL Then
Response.Redirect("default.asp")
End If
'If the user has just changed their password. Prompt them that it was successfully changed
If Request("changePasswd") = "true" Then
Response.Write("<script language='Javascript'>alert('Your Password Has been Successfully Changed!');</script>")
End If
Dim connection, cmd, objRS, latestDate, lastDateJPMC, firstDateJPMC, lastDateWACH, firstDateWACH, lastDateWFB, firstDateWFB, accountCount
Function calConvertDate(theDate)
Dim yr, mn, dy, dtSplit
dtSplit = Split(theDate,"/")
yr = dtSplit(2)
mn = dtSplit(0)
dy = dtSplit(1)
if Len(mn) = 1 then mn = "0" & mn
if Len(dy) = 1 then dy = "0" & dy
calConvertDate = "[" & yr & "," & mn & "]"
End Function
set connection = Server.CreateObject("adodb.connection")
connection.Open DBConn
connection.CommandTimeout = 60
set connection = Server.CreateObject("adodb.connection")
connection.Open DBConn
connection.CommandTimeout = 60
'Get Earliest & Latest Date in Database
If Err.Number = 0 Then
Response.Cookies("CLICK")=Err.number
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
Set .ActiveConnection = connection
.CommandText = "CIRS_Admin.spGetLatestDate"
.CommandType = adCmdStoredProc
set objRS = .Execute
End With
latestDate = calConvertDate(objRS("latestDate"))
Response.Cookies("latestdate")=objRS("latestDate")
objRS.Close
Set objRS = Nothing
Response.Cookies("BANG")=Err.number
End If
To debug, please add a statement like
Response.Write (objRS("latestDate"))
before the line
latestDate = calConvertDate(objRS("latestDate"))
so you can see if (for example) the date returned from the server has "-" as separator instead of "/" or if an empty value is returned.
After understanding what is causing the problem you can solve it
1.Where is this array that I'm supposedly attempting to reach a non-existent index of? I don't see it and the error gives no line number. Is there a chance something is not working correctly in the adodb library?
This is your array:
yr = dtSplit(2)
mn = dtSplit(0)
dy = dtSplit(1)
What's odd is that the same code is currently working in production. As far as I can tell they're configured identically (but I suspect there's a subtle difference that's causing this issue) and have identical code bases.
May be you have different regional settings?
I strongly suggest to you use better error handling.
Internal Server Error 500 w/ IIS Log

Asp OpenDB server not found

Get 505 error on this command on my asp file due to this code can you tell me fix of this please i will be thankful to you.
<%
OpenDB()
'Stmt = "SELECT * FROM articles WHERE status=1 "
Stmt = "SELECT * FROM articles, articleCategories WHERE articles.articleID=articleCategories.articleID AND categoryID=1 AND status=1"
set rsQuery=server.CreateObject("ADODB.Recordset")
rsQuery.Open Stmt, OpenDB, 1, 1
DO WHILE NOT rsQuery.EOF
wl "<div class=""listSub"">" & rsQuery("title") & "</div>"
rsQuery.MOVENEXT
LOOP
%>
First of all do a proper join:
SELECT * FROM articles join articleCategories
on articles.articleID=articleCategories.articleID
where TableName1.categoryID=1 AND TableName2.status=1
Because I do not know if both of your tables have any of those columns(categoryID,status) or just one but sure way to get an error is what you did.
Secon issue is how do you wrote your OpenDB sub. Need to see to tell you if issue is there or not.
And last but not least, do not do any fancy staff if you do not know how:
DO WHILE NOT rsQuery.EOF
response.write "<div class='"listSub"'><a href='articles.asp?article=" & rsQuery("articles.articleID") & "'>" & rsQuery("title") & "</a></div>"
rsQuery.MOVENEXT
LOOP

ASP - Loop while eof or bof - Select random product from idproduct but skip empty ids

I am trying to display a random product image and description from the access database, so i am selecting the highest idproduct then randomising a number between 1 and %highestid%, this is what i have so far....
IF frontpage = 1 then
SQLSTR = "SELECT idproduct AS prodtot FROM products order by idproduct desc"
Set objRS = Server.CreateObject("ADODB.Recordset")
SET objrs = oconn.execute(SQLSTR)
' Check result
Response.Write objRS("prodtot")
' attach
ntop = objRS("prodtot")
Randomize
' Generate random value between 1 and nTop .
nRandom= Int((nTop * Rnd) + 1)
sqlstr = "select * from products where idProduct = " & nRandom
response.Write"<br /><br />" & (sqlstr) & "<br /><br />"
'SET rs = oConn.execute(randomprod)
SET rs = oconn.execute(SQLSTR)
pranproddesc = rs("description")
response.Write(pranproddesc)
pranprodimg = rs("smallImageUrl")
end if
So far so good! But i have a problem, over time products have come and gone and I have alot of gaps in the %idproduct%, ive tried loop while rs.eof but it doesn't seem to do anythimg usefull, if anything at all. Just to clarify I have idproduct 1, 2, 5, 10, 11, 12 etc etc, so when it randomises idproduct3 it all goes up the spout! Can anyone help?
Thank you in advance! :)
Do the following:
IF frontpage = 1 then
Set objRS = Server.CreateObject("ADODB.Recordset")
SET objrs = oconn.execute(SQLSTR)
sqlSTR = "SELECT TOP 1 * FROM products ORDER BY NEWID()"
response.Write"<br /><br />" & (sqlstr) & "<br /><br />"
SET rs = oconn.execute(SQLSTR)
pranproddesc = rs("description")
response.Write(pranproddesc)
pranprodimg = rs("smallImageUrl")
end if
That sql will work in SQL Server:
Look at this page for SQL to return a random row for other databases:
http://www.petefreitag.com/item/466.cfm
Thanks, btw, I learnt something new figuring this out.
Rather than select a random ProductId, select a random row index from the recordset. That way you only have to hit the database once as well :)

Resources