Solved

Check if variable has been declared in ASP

Posted on 2003-10-23
6
605 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 51

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML and version of MS XML Dom Document versions 5 33
Sending ASP to server side 8 28
Want an individual results display div 8 40
Table header must be on top 2 25
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 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 informatio…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 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