Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Check if variable has been declared in ASP

Posted on 2003-10-23
6
604 Views
Last Modified: 2008-01-16
Hi,

Im writing a script and i want to check whether a variable has been declared or not. My database connection name is objConn, and so i want to check whether that variable has been declared, if it hasnt the user is on a page that has no database driven content and so i dont want to query the DB...

I use IsObject(objConn) to check whether the variable is an object but because i have option explicit on every page that returns an error...

I could turn on On Error Resume Next before the IsObject statement but i dont like to do this because i will never know if theres an error.

Any ideas or solutions anyone....

Thanks in advance
Al Higgs

0
Comment
Question by:higgsy
6 Comments
 
LVL 1

Expert Comment

by:justjuice
ID: 9605361
objToFind = "myobj"
objfound = false

For Each Item in Application.Contents
   If Item = objToFind Then
      objFound = true
      Exit For
   End If
Next

response.write("Object found?:" & objFound)
0
 
LVL 1

Expert Comment

by:justjuice
ID: 9605387
Actually, why couldn't you just dim "objConn" globally and make it equal nothing - setting as an object when required? IsOBject wouldnt fail then.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 9605399
Try define a function like below to check to see if the connection establish or not:

<%
      
      function isDBConnectionOk(dbpath)
      on error resume next
      set conn=server.CreateObject("adodb.connection")
      conn.Open "Provider=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE="+dbpath
      if conn.state = 1 then
            isDBConnectionOk = true
      else
            isDBConnectionOk = false
      end if
      conn.close
      set conn = nothing
      end function
%>

then

<%

 if isDBConnectionOk("E:\sites\domain\databases\domain.mdb") then
       'database connection ok
else
       'database connection failed
end if

%>
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Accepted Solution

by:
justjuice earned 500 total points
ID: 9605419
Here's the answer! the vbscript function 'VarType()' will tell what type the variable is - if it isnt declared it returns as 0.

http://www.devguru.com/technologies/vbscript/quickref/vartype.html
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9605611
higgsy,
You can use TypeName() too to detect what type of the variables.
Eg:
<%
 Dim x
 x=request("strName")
 Response.Write("VarType : " & TypeName(x))
%>

But, i believe after you put "Option Explicit" should alert for any undeclare variables. Then, you can know/fix which variables is still not declare.

Regards
x_com
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 9605626
As you are using Option Explicit, you have to declare the variable objConn if you want to check it's status.

I would rather put a constant in the include file that determines if the connection is opened:

Const blnHasDatabase=True

Then in the files without database connection, I would put:

Const blnHasDatabase=False

Then you simply check the constant for the status:

If blnHasDatabase Then
   ... do the query
End If
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question