How to tell whether a variable in ASP has been declared - asp-classic

Let me start off by saying I'm a PHP developer, not an ASP one. (And I really wish ASP had isset().) And I'm working in a live environment so I don't really have an opportunity to do any testing.
All the resources I've found suggest different ways to test for the existence of a variable.
Here's what I'm trying to do:
On SOME pages, I set a variable which holds a value for a robots <meta> tag:
dim dsep_robots
dsep_robots = "nofollow,noindex"
All pages include header.asp. In my header file, I want to test whether dsep_robots has a value and if so, output that value, otherwise, output nothing.
I think that testing whether dsep_robots has a value might look like this:
if not dsep_robots = "" then
'...
end if
Best practices in PHP state that when you're using a variable that may or may not exist, you should always test if (isset($var)) {...} (not doing so will trigger a Notice if the variable doesn't exist).
Is there such a thing in ASP -- i.e. do I really need to test if it exists, or can I simply test if it has a value?

ust by the way, your question isn't about classic ASP, it is a VBScript question. VBScript can occur in scripts outside of ASP. And compilation isn't done in VBScript, because it is an interpreted language. Nevermind.
I think there's some confusion here -- and your question seems to have more to do with uninitialized variables than undeclared variables. For undeclared variables, see below.
For uninitialized, try the function IsEmpty.
For checking for null, try the function IsNull.
dim x
x = 1
dim t
Response.write isempty(x)
Response.write "<br>"
Response.write isempty(t)
Will display:
False
True
Detecting Undeclared Variables
If you include Option Explicit in your header, use of a non-declared variable will cause an runtime error. If your script is not Option Explicit it will not generate an error, and there is no function that will tell you if the variable has been declared or not. This sounds sloppy, and it is, but it was intentional.
The only way you can escape this is to actually set Option Explicit and then trap the error that you will get when you try to use the undeclared variable. If you trap this particular error you will find that it has Err.Number = 500. So, the following will do what you want:
Option Explicit
dim x
On Error Resume Next
Response.Write dsep_robots
If Err.Number > 0 Then
Response.Write Err.Number
end if
Of course, if you set Option Explicit and your code is rife with undeclared variables then you'll get errors being thrown all over the place, so you'd need to set On Error Resume Next at the top of your code so you can successfully ignore it, and only trap it when you want to.
By the way, here's Microsoft's online reference for VBScript:
http://msdn.microsoft.com/en-us/library/d1wf56tt(v=VS.85).aspx

#Jazzerus: I'd recommend putting the code within header.asp into a Sub, something like
Sub outputHeader(ByRef MyTitle, Byref dsep_robots)
'contents of header.asp
End Sub
...and then in your calling pages include header.asp right at the top and use
outputHeader "Title for this page", "value you want dsep_robots to have for page"
If you don't set dsep_robots on that page, then just leave the second parameter blank ("")
Then just checking if the variable is empty or not within the Sub should suffice:
If dsep_robots <> "" Then
Response.Write dsep_robots
End If

What about:
If NOT IsEmpty(myvariable) Then...
that seems to have been working for me.

I use the VarType function to detect if the variable is defined. If the tested variable is not defined, VarType returns the value vbError (10). Interesting to note that the optional parameter cannot be the last parameter else ASP tosses an error.
function Sample(p1,p2,p3,p4)
if VarType(p3) <> vbError then
'do something with p3
end if
end function
ThisWorks=Sample(1,2,,3)
ThisFails=Sample(1,2,3,)

Related

Handling Chinese in ASP classic

I write the following piece of codes :
rst.Open(strSQL & Request.QueryString("C-PLACE")), conn, 0, 1
But got the following error. However, if the querystring is in English or just number, no error will pop out. Any guru can help please ?
Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
/deliverable/GetMemberTest.asp, line 19
It's going to either be passing an encoding variable to the server, or in the case of your error, its saying "too few parameters". In this case, the parameter is "C-PLACE" and its suppose to be passed to your asp script from the previous page's link, something like:
/deliverable/GetMemberTest.asp?C-PLACE=THECPLACE
https://www.w3schools.com/asp/coll_querystring.asp
(citation about query strings)
or something like that .. obviously its not actually "THECPLACE", but just saying a QueryString("VARIABLENAME") looks to the URL of the previous page to pass the parameter to the script, so that error message should of done something to add a ? mark = C-PLACE= to something, and we aren't seeing that. So something on the previous page that was suppose to add this when they click a submit button didn't do it's job, or the script is just getting run on its own without the proper previous page's work being done to prepare it to execute properly on the following page.
It will also be of note that these types of things are easily hacked through sql script injection, so if you aren't validating your url first, someone could use some code to escape out of your sql and add their own code, such as one to drop your tables ..., so make sure you validate the variable FIRST instead of dumping it straight into your code. I can give some guidance into that later, but first lets figure out your problem.
(side note - can i request strSQL from you? Can you put this line in before that line:
<%
response.write("strSQL is " & StrSQL & "<BR>")
%>
All this code does is display what is stored in the StrSQL variable, see if we can figure out what is going on here. Also take note that your error message indicated that it expected 2 parameters, so we are missing 2 for this script to run properly.
EDIT - try this encoding:
<%
Response.CodePage=65001
Response.Charset="UTF-8"
Response.ContentType = "text/html"
%>
Try this strSQL, you didn't need the Response.Write and on C-PLACE you want to use '' instead of "" because the "" will exit you out of the SQL statement. Try this, and let me know how it works, but I still think we are going to need another parameter supplied to it, unless its getting one from the string and then it isn't actually counting the one supplied from the url perhaps.
<%
strSQL="SELECT * FROM DetailMemberInfo
WHERE C-PLACE=" & strSQL & Request.QueryString('C-PLACE'))"
%>

comparing http_referer against http_host

Hi
I need to check whether the http_referer is the same site as the current site.
I have the following code
Dim strReferer As String
strReferer = Request.ServerVariables("HTTP_REFERER")
If strReferer.Contains(Request.ServerVariables("HTTP_HOST")) then
'do task
End If
This is throwing up an error saying - "Object reference not set to an instance of an object." and flagging the if line as the offending line of code.
Any ideas where I'm going wrong?
MY SOLUTION:
strReferer = "" & Request.ServerVariables("HTTP_REFERER")
Means the string always has a value even if it's nothing.
Because HTTP_REFERER is not always populated - only if you have clicked a link. So if you browse directly to a page, that header will be empty.
It's possible for Request.ServerVariables("HTTP_REFERER") to be null, so you should check for this when assigning the variable.
If Not String.IsNullOrEmpty(Request.ServerVariables("HTTP_REFERER"))
'do your stuff

Check if an Object exists in VBScript

I'm maintaining a Classic ASP app written in VB Script by an outside company long, long ago.
I have an array of imagefile paths, like so:
dim banners, arrKeys, i
set banners=CreateObject("Scripting.Dictionary")
banners.Add "banner1.jpg", "http://www.somelink.com"
banners.Add "banner2.jpg", "http://www.somelink.com"
banners.Add "banner3.jpg", "http://www.somelink.com"
This will exist ONLY on pages that have banner ads. There is some standard code that iterates through this list in an include file (common to all pages).
If Not banners Is Nothing then
' then loop through the Dictionary and make a list of image links
End if
The problem is that if banners is not instantiated on the page (it's not on all pages), I get a Can't find object error
What's the proper way to check if an object exists in VB Script?
#Atømix: Replace
If Not banners Is Nothing then
and use
If IsObject(banners) Then
Your other code you can then place into an include file and use it at the top of your pages to avoid unnecessary duplication.
#Cheran S: I tested my snippets above with Option Explicit on/off and didn't encounter errors for either version, regardless of whether Dim banners was there or not. :-)
IsObject could work, but IsEmpty might be a better option - it is specifically intended to check if a variable exists or has been initialised.
To summarize:
IsEmpty(var) will test if a variable exists (without Object Explicit), or is initialised
IsNull(var) will test if a variable has been assigned to Null
var Is Nothing will test if a variable has been Set to Nothing, but will throw an error if you try it on something that isn't an object
IsObject(var) will test if a variable is an object (and will apparently still return False if var is Empty).
If a variable is declared, but not initialized, its value will be Empty, which you can check for with the IsEmpty() function:
Dim banners
If IsEmpty(banners) Then
Response.Write "Yes"
Else
Response.Write "No"
End If
' Should result in "Yes" being written
banners will only be equal to Nothing if you explicitly assign it that value with Set banners = Nothing.
You will have problems, though, with this technique if you have Option Explicit turned on (which is the recommendation, but isn't always the case). In that case, if banners hasn't been Dimed and you try to test IsEmpty(banners), you will get a runtime error. If you don't have Option Explicit on, you shouldn't have any problems.
edit: I just saw this related question and answer which might help, too.
Somewhat related is IsMissing() to test if an optional parameter was passed, in this case an object, like this:
Sub FooBar(Optional oDoc As Object)
'if parameter is missing then simulate it
If IsMissing(oDoc) Then Dim oDoc as Object: oDoc = something
...
You need to have at least dim banners on every page.
Don't you have a head.asp or something included on every page?
Neither of IsEmpty, Is Object, IsNull work with the "Option Explicit" Setting, as stealthyninja above has misleadingly answered.
The single way i know is to 'hack' the 'Option Explicit' with the 'On Error Resume Next' setting, as Tristan Havelick nicely does it here:
Is there any way to check to see if a VBScript function is defined?

Conditional includes in Classic ASP - where the file may not exist on the server

I am currently in a situation where I have to make some additions to an application written in classic ASP using server-side JScript on IIS.
The additions that I need to make involve adding a series of includes to the server-side code to extend the application's capabilities. However, the inc files may not exist on the server in all cases, so I need the application to fall back to the existing behavior (ignore the includes) if the files do not exist, rather than generating an error.
I know that this can't be accomplished using if statements in the JScript code because of the way that SSI works, and have not come across any ways of dynamically including the code on the server side, where the files may not exist.
Does anyone know of a way to accomplish this in classic ASP? Any help would be much appreciated.
Here's a script to dynamically include asp files:
<%
' **** Dynamic ASP include v.2
function fixInclude(content)
out=""
if instr(content,"#include ")>0 then
response.write "Error: include directive not permitted!"
response.end
end if
content=replace(content,"<"&"%=","<"&"%response.write ")
pos1=instr(content,"<%")
pos2=instr(content,"%"& ">")
if pos1>0 then
before= mid(content,1,pos1-1)
before=replace(before,"""","""""")
before=replace(before,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
before=vbcrlf & "response.write """ & before & """" &vbcrlf
middle= mid(content,pos1+2,(pos2-pos1-2))
after=mid(content,pos2+2,len(content))
out=before & middle & fixInclude(after)
else
content=replace(content,"""","""""")
content=replace(content,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
out=vbcrlf & "response.write """ & content &""""
end if
fixInclude=out
end function
Function getMappedFileAsString(byVal strFilename)
Dim fso,td
Set fso = Server.CreateObject("Scripting.FilesystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(strFilename), 1)
getMappedFileAsString = ts.ReadAll
ts.close
Set ts = nothing
Set fso = Nothing
End Function
execute (fixInclude(getMappedFileAsString("included.asp")))
%>
The last line (the one starting with "execute") is equivalent to an "include" directive, with the difference that it can be included inside an "if" statement (dynamic include).
Bye
If you are really brave, you can read the contents of the file and then Eval() it.
But you will have not real indication of line numbers if anything goes wrong in the included code.
As a potentially better alternative: Can you not create some sanity check code in global.asa to create the include files as blanks if they do not exist?
Put simply, no. Why would the files not exist? Can you not at least have empty files present?
What you could do is something like this:
Use Scripting.FileSystemObject to detect the presence of the files
Use Server.Exeecute to "include" the files, or at least execute the code.
The only problem is that the files cannot share normal program scope variables.
The solution to this turned out to be to use thomask's suggestion to include the files and to set a session variable with a reference to "me" as per http://www.aspmessageboard.com/showthread.php?t=229532 to allow me to have access to the regular program scope variables.
(I've registered because of this, but can't seem to associate my registered account with my unregistered account)

asp errors not displayed

I have moved an sql database from one server to a new one (detached/attached)
Now i experience some strange behavior as it does not work but NO error is displayed.
This is the code
<%
const database_dsn="PROVIDER=SQLNCLI10; SERVER=FR-2626\SQLLOP;DATABASE=Lop;Uid=admin-sql;Pwd=xxxx;"
response.write "Step 0//"
set conn=server.CreateObject("ADODB.Connection")
set RS=server.CreateObject("ADODB.Recordset")
conn.Open database_dsn
response.write "Step 1//"
req = "Select count(*) From tblArticleList"
response.write "Step 2//"
set RS = conn.Execute(req)
response.write "Step 3//"
%>
The program stops at Step 2; then nothing, no error is displayed...
I just don t know what to do..How can i get some error?
Thanks
Jonathan
Oh i partially found the answer for the error display.
In the Debug pane of the IIS directory configuration, Enable ASP debugging should NOT be checked...althought i thougth it should...
have you got your browser set to "show friendly http errors" this in conjuction with what you have already identified is the common causes I've had for not seeing an error message.
Shahkaplesh is also right that you can use Server.GetLastError() to get the last error that occurred but you shouldn't need to do this in this example.
When executing a query you don't use the "Set" command. I don't know why its not showing you anything, but your code should look more like this:
<%
const database_dsn="PROVIDER=SQLNCLI10; SERVER=FR-2626\SQLLOP;DATABASE=Lop;Uid=admin-sql;Pwd=xxxx;"
response.write("Step 0//")
set conn=server.CreateObject("ADODB.Connection")
set RS=server.CreateObject("ADODB.Recordset")
conn.Open database_dsn
response.write("Step 1//")
req = "Select count(*) From tblArticleList"
response.write("Step 2//")
RS = conn.Execute(req)
response.write("Step 3//")
%>
Yes, the parentheses on the "Response.Write" are optional. But I'm OCD like that and it makes troubleshooting a little easier.
You need to place error checking code to find out what the actual error might be.
I would suggest that you change you code like so:
<%
const database_dsn="PROVIDER=SQLNCLI10; SERVER=FR-2626\SQLLOP;DATABASE=Lop;Uid=admin- sql;Pwd=xxxx;"
'Its very important to add this line!!! '
On Error Resume Next
'Its very important to add this line!!! '
response.write "Step 0//"
set conn=server.CreateObject("ADODB.Connection")
set RS=server.CreateObject("ADODB.Recordset")
conn.Open database_dsn
if err.number<>0 then
response.write err.description
end if
response.write "Step 1//"
req = "Select count(*) From tblArticleList"
response.write "Step 2//"
set RS = conn.Execute(req)
if err.number<>0 then
response.write err.description
end if
response.write "Step 3//"
%>
And not to kick a dead horse but I'm doing something similar against an Oracle database and I've had two phantom problems I have yet to identify root cause but here's two things that made them go away.
1. Name all columns in a Query and Alias any that are calculated (sum, count, avg, etc.) So your query would become
req = "Select count(*) NumRows From tblArticleList"
2. Wrapping my query string in a call to cstr caused the result.EOF flag to be populated correctly rather than be returned with an empty or null value causing a simple DO WHILE NOT result.EOF Some Action LOOP to create an infinite loop until the web request timed out. So e.g.
response.write "Step 2//"
set RS = conn.Execute(cstr(req))
Nothing major just a couple tips if you get stuck and can't find out why. Follow the debugging advice above though, that's good info.
I think Server has a GetLastError method, which you can check to find out what error occurred while running any statement.
e.g. On error resume next
.... statement that could cause an error....
errorObject = Server.GetLastError()
For ASPError object, refer http://www.w3schools.com/asp/asp_ref_error.asp
Actually don't mean to be disagreeable, but yes you do need a Set there, as you are setting an object type and presumably wanting to use the return value at some point (if this wasn't just a test script which it looks like to me).
Also btw parentheses are not really correct there in Response.Write() as it does not return a value. They only happen to work on single parameter subs because you can put parentheses anywhere you like around expressions.
eg:
a = (b)
Response.Write ((("test"))&(1))

Resources