Solved

Check if variable has been declared in ASP

Posted on 2003-10-23
6
601 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 49

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now