Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if variable has been declared in ASP

Posted on 2003-10-23
6
Medium Priority
?
608 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
[X]
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
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 53

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Accepted Solution

by:
justjuice earned 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

661 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