Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Variable Scope and Functions

Reply
Thread Tools

Variable Scope and Functions

 
 
tegryan@gmail.com
Guest
Posts: n/a
 
      07-11-2006
Hey All,

I'm trying to do something that should be very simple, but I think I've
been up for too many hours or something.

Basically, I have an asp application that uses a database connection,
and I want to declare (create) that connection in an include and use it
throughout the application in various functions. I've tried making the
connection object a public variable, a constant, making the functions
public, passing the connection object in byRef, byVal, etc... I just
can't get it to work. Is this not possible in classic ASP?

I should also mention that if I take this code out of the function it
works as expected.

Thanks!

Teg Ryan

Here is a sample of what i'm trying to do:

Include:

<%

public cn
public rs
public sql

set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
DBQ=[[path]];"

%>

Then here's one of the functions in the ASP file that has the include
at the top:

Function SQLSelect()

sql = "select * from TABLE"
rs.open sql,cn,3,3
... etc

End function

 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      07-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hey All,
>
> I'm trying to do something that should be very simple, but I think
> I've been up for too many hours or something.
>
> Basically, I have an asp application that uses a database connection,
> and I want to declare (create) that connection in an include and use
> it throughout the application in various functions. I've tried
> making the connection object a public variable, a constant, making
> the functions public, passing the connection object in byRef, byVal,
> etc... I just can't get it to work. Is this not possible in classic
> ASP?
>
> I should also mention that if I take this code out of the function it
> works as expected.
>
> Thanks!
>
> Teg Ryan
>
> Here is a sample of what i'm trying to do:
>
> Include:
>
> <%
>
> public cn
> public rs
> public sql
>
> set cn = Server.CreateObject("ADODB.Connection")
> set rs = Server.CreateObject("ADODB.RecordSet")
> cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
> DBQ=[[path]];"


Nothing to do with your problem, but:
http://www.aspfaq.com/show.asp?id=2126

>
> %>
>
> Then here's one of the functions in the ASP file that has the include
> at the top:
>
> Function SQLSelect()
>
> sql = "select * from TABLE"
> rs.open sql,cn,3,3
> ... etc
>
> End function


This should work. What happens when you try it?

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
 
 
 
tegryan@gmail.com
Guest
Posts: n/a
 
      07-11-2006

Hey Bob, thanks for the quick reply!

Firstly, I thought I was using a native OLEDB provider....lol! What am
I doing? I haven't created a DSN for that box, and it doesn't look
like the DSNless connections I'm used to...

Secondly and more importantantly, I forgot to leave out the error
message I am getting:

Error Type:
Microsoft VBScript runtime (0x800A01A
Object required: 'rs'

I am going to double check right now, but I believe that if I copy the
connection code into the function in question, it works fine.

Thanks!

Teg

Bob Barrows [MVP] wrote:
> (E-Mail Removed) wrote:
> > Hey All,
> >
> > I'm trying to do something that should be very simple, but I think
> > I've been up for too many hours or something.
> >
> > Basically, I have an asp application that uses a database connection,
> > and I want to declare (create) that connection in an include and use
> > it throughout the application in various functions. I've tried
> > making the connection object a public variable, a constant, making
> > the functions public, passing the connection object in byRef, byVal,
> > etc... I just can't get it to work. Is this not possible in classic
> > ASP?
> >
> > I should also mention that if I take this code out of the function it
> > works as expected.
> >
> > Thanks!
> >
> > Teg Ryan
> >
> > Here is a sample of what i'm trying to do:
> >
> > Include:
> >
> > <%
> >
> > public cn
> > public rs
> > public sql
> >
> > set cn = Server.CreateObject("ADODB.Connection")
> > set rs = Server.CreateObject("ADODB.RecordSet")
> > cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
> > DBQ=[[path]];"

>
> Nothing to do with your problem, but:
> http://www.aspfaq.com/show.asp?id=2126
>
> >
> > %>
> >
> > Then here's one of the functions in the ASP file that has the include
> > at the top:
> >
> > Function SQLSelect()
> >
> > sql = "select * from TABLE"
> > rs.open sql,cn,3,3
> > ... etc
> >
> > End function

>
> This should work. What happens when you try it?
>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.


 
Reply With Quote
 
tegryan@gmail.com
Guest
Posts: n/a
 
      07-11-2006

yup, works just fine when I put the connection information in the
function.

Also, I should mention that I'm using Access, not SQL Server. Not sure
if that matters...

(E-Mail Removed) wrote:
> Hey Bob, thanks for the quick reply!
>
> Firstly, I thought I was using a native OLEDB provider....lol! What am
> I doing? I haven't created a DSN for that box, and it doesn't look
> like the DSNless connections I'm used to...
>
> Secondly and more importantantly, I forgot to leave out the error
> message I am getting:
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A
> Object required: 'rs'
>
> I am going to double check right now, but I believe that if I copy the
> connection code into the function in question, it works fine.
>
> Thanks!
>
> Teg
>
> Bob Barrows [MVP] wrote:
> > (E-Mail Removed) wrote:
> > > Hey All,
> > >
> > > I'm trying to do something that should be very simple, but I think
> > > I've been up for too many hours or something.
> > >
> > > Basically, I have an asp application that uses a database connection,
> > > and I want to declare (create) that connection in an include and use
> > > it throughout the application in various functions. I've tried
> > > making the connection object a public variable, a constant, making
> > > the functions public, passing the connection object in byRef, byVal,
> > > etc... I just can't get it to work. Is this not possible in classic
> > > ASP?
> > >
> > > I should also mention that if I take this code out of the function it
> > > works as expected.
> > >
> > > Thanks!
> > >
> > > Teg Ryan
> > >
> > > Here is a sample of what i'm trying to do:
> > >
> > > Include:
> > >
> > > <%
> > >
> > > public cn
> > > public rs
> > > public sql
> > >
> > > set cn = Server.CreateObject("ADODB.Connection")
> > > set rs = Server.CreateObject("ADODB.RecordSet")
> > > cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
> > > DBQ=[[path]];"

> >
> > Nothing to do with your problem, but:
> > http://www.aspfaq.com/show.asp?id=2126
> >
> > >
> > > %>
> > >
> > > Then here's one of the functions in the ASP file that has the include
> > > at the top:
> > >
> > > Function SQLSelect()
> > >
> > > sql = "select * from TABLE"
> > > rs.open sql,cn,3,3
> > > ... etc
> > >
> > > End function

> >
> > This should work. What happens when you try it?
> >
> > --
> > Microsoft MVP -- ASP/ASP.NET
> > Please reply to the newsgroup. The email account listed in my From
> > header is my spam trap, so I don't check it very often. You will get a
> > quicker response by posting to the newsgroup.


 
Reply With Quote
 
tegryan@gmail.com
Guest
Posts: n/a
 
      07-11-2006

Shoot, one more thing. The file with the function in it is also
another include. In my head this shouldn't matter, but maybe one of
you jeanyuses can help me out here....

The basic structure is this:

main.asp (contains include statements to the other two)
include1.asp (the connection string, set as public)
include2.asp (the function using the connection string that doesn't
work)

Thanks,

Teg Ryan

(E-Mail Removed) wrote:
> yup, works just fine when I put the connection information in the
> function.
>
> Also, I should mention that I'm using Access, not SQL Server. Not sure
> if that matters...
>
> (E-Mail Removed) wrote:
> > Hey Bob, thanks for the quick reply!
> >
> > Firstly, I thought I was using a native OLEDB provider....lol! What am
> > I doing? I haven't created a DSN for that box, and it doesn't look
> > like the DSNless connections I'm used to...
> >
> > Secondly and more importantantly, I forgot to leave out the error
> > message I am getting:
> >
> > Error Type:
> > Microsoft VBScript runtime (0x800A01A
> > Object required: 'rs'
> >
> > I am going to double check right now, but I believe that if I copy the
> > connection code into the function in question, it works fine.
> >
> > Thanks!
> >
> > Teg
> >
> > Bob Barrows [MVP] wrote:
> > > (E-Mail Removed) wrote:
> > > > Hey All,
> > > >
> > > > I'm trying to do something that should be very simple, but I think
> > > > I've been up for too many hours or something.
> > > >
> > > > Basically, I have an asp application that uses a database connection,
> > > > and I want to declare (create) that connection in an include and use
> > > > it throughout the application in various functions. I've tried
> > > > making the connection object a public variable, a constant, making
> > > > the functions public, passing the connection object in byRef, byVal,
> > > > etc... I just can't get it to work. Is this not possible in classic
> > > > ASP?
> > > >
> > > > I should also mention that if I take this code out of the function it
> > > > works as expected.
> > > >
> > > > Thanks!
> > > >
> > > > Teg Ryan
> > > >
> > > > Here is a sample of what i'm trying to do:
> > > >
> > > > Include:
> > > >
> > > > <%
> > > >
> > > > public cn
> > > > public rs
> > > > public sql
> > > >
> > > > set cn = Server.CreateObject("ADODB.Connection")
> > > > set rs = Server.CreateObject("ADODB.RecordSet")
> > > > cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver (*.mdb)};
> > > > DBQ=[[path]];"
> > >
> > > Nothing to do with your problem, but:
> > > http://www.aspfaq.com/show.asp?id=2126
> > >
> > > >
> > > > %>
> > > >
> > > > Then here's one of the functions in the ASP file that has the include
> > > > at the top:
> > > >
> > > > Function SQLSelect()
> > > >
> > > > sql = "select * from TABLE"
> > > > rs.open sql,cn,3,3
> > > > ... etc
> > > >
> > > > End function
> > >
> > > This should work. What happens when you try it?
> > >
> > > --
> > > Microsoft MVP -- ASP/ASP.NET
> > > Please reply to the newsgroup. The email account listed in my From
> > > header is my spam trap, so I don't check it very often. You will get a
> > > quicker response by posting to the newsgroup.


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      07-11-2006
(E-Mail Removed) wrote:
> Hey Bob, thanks for the quick reply!
>
> Firstly, I thought I was using a native OLEDB provider....lol!


Nope. You're using the MSDASQL provider, which is the provider for ODBC
databases.

> What
> am I doing? I haven't created a DSN for that box, and it doesn't look
> like the DSNless connections I'm used to...


Well, that's what it is: a DSNless connection
>
> Secondly and more importantantly, I forgot to leave out the error
> message I am getting:
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A
> Object required: 'rs'
>
> I am going to double check right now, but I believe that if I copy the
> connection code into the function in question, it works fine.
>
> Thanks!
>
> Teg


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      07-11-2006
No, the data source should not matter.
I'm going to try and reproduce this.

(E-Mail Removed) wrote:
> yup, works just fine when I put the connection information in the
> function.
>
> Also, I should mention that I'm using Access, not SQL Server. Not
> sure if that matters...
>
> (E-Mail Removed) wrote:
>> Hey Bob, thanks for the quick reply!
>>
>> Firstly, I thought I was using a native OLEDB provider....lol! What
>> am I doing? I haven't created a DSN for that box, and it doesn't
>> look like the DSNless connections I'm used to...
>>
>> Secondly and more importantantly, I forgot to leave out the error
>> message I am getting:
>>
>> Error Type:
>> Microsoft VBScript runtime (0x800A01A
>> Object required: 'rs'
>>
>> I am going to double check right now, but I believe that if I copy
>> the connection code into the function in question, it works fine.
>>
>> Thanks!
>>
>> Teg
>>
>> Bob Barrows [MVP] wrote:
>>> (E-Mail Removed) wrote:
>>>> Hey All,
>>>>
>>>> I'm trying to do something that should be very simple, but I think
>>>> I've been up for too many hours or something.
>>>>
>>>> Basically, I have an asp application that uses a database
>>>> connection, and I want to declare (create) that connection in an
>>>> include and use it throughout the application in various
>>>> functions. I've tried making the connection object a public
>>>> variable, a constant, making the functions public, passing the
>>>> connection object in byRef, byVal, etc... I just can't get it to
>>>> work. Is this not possible in classic ASP?
>>>>
>>>> I should also mention that if I take this code out of the function
>>>> it works as expected.
>>>>
>>>> Thanks!
>>>>
>>>> Teg Ryan
>>>>
>>>> Here is a sample of what i'm trying to do:
>>>>
>>>> Include:
>>>>
>>>> <%
>>>>
>>>> public cn
>>>> public rs
>>>> public sql
>>>>
>>>> set cn = Server.CreateObject("ADODB.Connection")
>>>> set rs = Server.CreateObject("ADODB.RecordSet")
>>>> cn.Open "Provider=MSDASQL; Driver={Microsoft Access Driver
>>>> (*.mdb)}; DBQ=[[path]];"
>>>
>>> Nothing to do with your problem, but:
>>> http://www.aspfaq.com/show.asp?id=2126
>>>
>>>>
>>>> %>
>>>>
>>>> Then here's one of the functions in the ASP file that has the
>>>> include at the top:
>>>>
>>>> Function SQLSelect()
>>>>
>>>> sql = "select * from TABLE"
>>>> rs.open sql,cn,3,3
>>>> ... etc
>>>>
>>>> End function
>>>
>>> This should work. What happens when you try it?
>>>
>>> --
>>> Microsoft MVP -- ASP/ASP.NET
>>> Please reply to the newsgroup. The email account listed in my From
>>> header is my spam trap, so I don't check it very often. You will
>>> get a quicker response by posting to the newsgroup.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
tegryan@gmail.com
Guest
Posts: n/a
 
      07-11-2006

Lol! Well if you can't solve this for me, at least you've taught me
something already.

To replicate it, all you need to do is create a DSNless connection like
the one I have, put it in an include and try to use the rs/cn object
from inside a function. I've narrowed it down to that behaviour, but
can't get past it without doing something ugly....

Bob Barrows [MVP] wrote:
> (E-Mail Removed) wrote:
> > Hey Bob, thanks for the quick reply!
> >
> > Firstly, I thought I was using a native OLEDB provider....lol!

>
> Nope. You're using the MSDASQL provider, which is the provider for ODBC
> databases.
>
> > What
> > am I doing? I haven't created a DSN for that box, and it doesn't look
> > like the DSNless connections I'm used to...

>
> Well, that's what it is: a DSNless connection
> >
> > Secondly and more importantantly, I forgot to leave out the error
> > message I am getting:
> >
> > Error Type:
> > Microsoft VBScript runtime (0x800A01A
> > Object required: 'rs'
> >
> > I am going to double check right now, but I believe that if I copy the
> > connection code into the function in question, it works fine.
> >
> > Thanks!
> >
> > Teg

>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      07-11-2006
(E-Mail Removed) wrote:
> yup, works just fine when I put the connection information in the
> function.
>


Here's my first repro attempt, which works with no error:

ins.asp:
<%
option explicit

public cn
public rs
public sql

set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "Provider=microsoft.jet.oledb.4.0;" & _
"data source=" & server.MapPath("xxxxx.mdb")
%>

insuse.asp:
<!--#include file="ins.asp"-->
<%
function useconnection()
sql="select top 1 * from mytable"
rs.open sql,cn,,,1
useconnection=rs(0).value
end function
response.write useconnection
rs.close:set rs=nothing
cn.close:set cn=nothing
%>

I'll try your other scenario later.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      07-11-2006
(E-Mail Removed) wrote:
> yup, works just fine when I put the connection information in the
> function.
>
> Also, I should mention that I'm using Access, not SQL Server. Not
> sure if that matters...
>


And here is my second repro attempt, which again works just fine:
ins.asp:
<%
option explicit

public cn
public rs
public sql

set cn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cn.Open "Provider=microsoft.jet.oledb.4.0;" & _
"data source=" & server.MapPath("xxxxx.mdb")
%>

ins2.asp:
<%
function useconnection()
sql="select top 1 * from mytable"
rs.open sql,cn,,,1
useconnection=rs(0).value
end function
%>



insuse.asp:
<!--#include file="ins.asp"-->
<!--#include file="ins2.asp"-->
<%
response.write useconnection
rs.close:set rs=nothing
cn.close:set cn=nothing
%>


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Objects, Callback functions and Variable Scope Funktopia Javascript 2 11-15-2005 12:16 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments