ASP Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreASP Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 9th, 2003, 12:48 AM
zimm zimm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 30 zimm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
returning objects from functions

Is there any way to return an object from a function in ASP?

I've done it a zillion times in VB, but can't figure it out in ASP. Seems to me that it shouldn't be a problem because all variables are variants (references), so ASP shouldn't care what's being passed.

For example

Function GetCon()

dim cn, cnstring
set cn = server.createobject("adodb.connection")
cn.open (cnstring)
GetCon = cn

end function



Function GetRecord
dim rs
set rs = server.createobject("adodb.recordset")
set rs.activeconnection = GetCon()
more code
end function

*******************

it bombs on the line where I set the recordset's active connection because it says that there is no open connection to the DB. Well DUH.

Any thoughts?

Reply With Quote
  #2  
Old August 9th, 2003, 09:04 AM
Vlince Vlince is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Canada, Quebec, Montreal
Posts: 410 Vlince User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Well the reason why you're getting the error message is simple, look here:

Function GetCon()

dim cn, cnstring
set cn = server.createobject("adodb.connection")
cn.open (cnstring)
GetCon = cn

end function

NOTICE that you've Dim'ed the variable cnstring but you haven't given it any value.

What you can/could do is pass the connection string as a parameter to the GetCon function such as:



Function GetCon(ByVal TheConnectionString)

dim cn
set cn = server.createobject("adodb.connection")
cn.open (TheConnectionString)
GetCon = cn

end function




One thing you could have done to trap the error would've been to put some error handling inside the GetCon() function such as:

Function GetCon()
On Error Resume Next
dim cn, cnstring
set cn = server.createobject("adodb.connection")
cn.open (cnstring)
GetCon = cn


'FOR DEBUG ONLY
If Err.number <> 0 Then
Response.Write "Number : " & Err.number & "<br>"
Response.Write "Description : " & Err.Description & "<br>"
Response.Write "Source : " & Err.Source & "<br>"
Response.End
End If

end function


The error will be traped if any...


Hope this helps!
Sincerely

Vlince

Reply With Quote
  #3  
Old August 9th, 2003, 10:07 AM
zimm zimm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 30 zimm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
Originally posted by Vlince
Well the reason why you're getting the error message is simple, look here:

Function GetCon()

dim cn, [b]cnstring

set cn = server.createobject("adodb.connection")
cn.open (cnstring)
GetCon = cn

end function

NOTICE that you've Dim'ed the variable cnstring but you haven't given it any value.


I didn't initialize it (in this example on this BBS) for expediency reasons.


Quote:
What you can/could do is pass the connection string as a parameter to the GetCon function such as:



Function GetCon(ByVal TheConnectionString)

dim cn
set cn = server.createobject("adodb.connection")
cn.open (TheConnectionString)
GetCon = cn

end function



The error occurs in the function call to GetCon in the GetRecord function. Passing the connection string to the GetCon function doesn't work either, because the error I'm getting occurs because the ASP doesn't recognize the GetCon function as a connection object.


Quote:
One thing you could have done to trap the error would've been to put some error handling inside the GetCon() function such as:

Function GetCon()
On Error Resume Next
dim cn, cnstring
set cn = server.createobject("adodb.connection")
cn.open (cnstring)
GetCon = cn


'FOR DEBUG ONLY
If Err.number <> 0 Then
Response.Write "Number : " & Err.number & "<br>"
Response.Write "Description : " & Err.Description & "<br>"
Response.Write "Source : " & Err.Source & "<br>"
Response.End
End If

end function


I'm not worried about error trapping per se in this example, because I know what my error is. I just need to know why this function call isn't working (because it works in VB)


Thanks for the help.

BTW, here is my actual code:
Quote:
<script language = "vbscript" runat = "server">

function GetCon() as vbdataobject
dim cn, strcn

strcn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\lonestar\data\lonestar.mdb;Persist Security Info=False"

set cn = server.createobject("adodb.connection")

cn.open(strcn)

GetCon = cn
end function



dim rs, sql

set rs = server.createobject("adodb.recordset")

sql = "select * from swimmer"

set rs.activeconnection = GetCon()

rs.open(sql)

do while not rs.eof and not rs.bof
response.write(rs(0) & ", " & rs(1) & " " & rs(2) & " " & rs(3) & " " & rs(4) & " " & rs(5) & " " & rs(6))
response.write("<br><br>")
rs.movenext
loop


</script>


My error is occurring in the bold line, before the Function can even be executed.

Reply With Quote
  #4  
Old August 9th, 2003, 11:32 AM
nopoints nopoints is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Windsor ON, Canada
Posts: 459 nopoints User rank is Corporal (100 - 500 Reputation Level)nopoints User rank is Corporal (100 - 500 Reputation Level)nopoints User rank is Corporal (100 - 500 Reputation Level)nopoints User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 44 m 22 sec
Reputation Power: 8
because of default properties setting the value of an object to another object assumes the default property. since that occurs you need a way of referencing the entire object. that is what the Set command is for. you are trying to return an object and cn is an object therefore you need to reference the entire object and not the default property.
Code:
' change
GetCon = cn
' to
Set GetCon = cn
__________________
Programmer's Corner

Reply With Quote
  #5  
Old August 9th, 2003, 09:28 PM
zimm zimm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 30 zimm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
AAARRRGGGGHHHH

I knew that.


Quote:
Originally posted by nopoints
because of default properties setting the value of an object to another object assumes the default property. since that occurs you need a way of referencing the entire object. that is what the Set command is for. you are trying to return an object and cn is an object therefore you need to reference the entire object and not the default property.
Code:
' change
GetCon = cn
' to
Set GetCon = cn

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreASP Programming > returning objects from functions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway