Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is this a correct function call?

Status
Not open for further replies.

stemitch2000

Programmer
Nov 12, 2003
47
0
0
GB
I have a function that sets up a connection to a database.

function OpenDB
dim con
On Error Resume Next
If Not (IsObject(con)) Then
response.write "opening new connection"
Set con = Server.CreateObject("ADODB.Connection")
set session("con") = con
strDSN = Session("strDSN")
con.Open "DSN=" & strDSN
Else
'do nadda!
End If

openDB = con
End function

when I go to call it...

set con = openDB

is this right? because it comes back with a error 424 object required
 
As i was telling in the other post the code should look like this
Code:
function OpenDB
 dim con
 If session("con")="" Then
    response.write "opening new connection"
    Set con = Server.CreateObject("ADODB.Connection")
    strDSN = Session("strDSN")
    con.Open "DSN=" & strDSN
    set session("con") = con
 Else
    con=session("con")
 End If     
 openDB = con
End function
'function call...
set con = openDB


________
George, M
 
Still get the same error

the function is this, added error handling in it.

function OpenDB
dim con
On Error Resume Next
err.clear
If session("con")="" Then
response.write "opening new connection"
Set con = Server.CreateObject("ADODB.Connection")
strDSN = Session("strDSN")
if err.number <> 0 then
response.write err.number
response.write err.description
response.write &quot;before open con&quot;
end if
con.Open &quot;DSN=&quot; & strDSN
set session(&quot;con&quot;) = con
if err.number <> 0 then
response.write err.number
response.write err.description
response.write &quot;after open con&quot;
end if
response.write &quot;opened new session successfully&quot;
Else
con=session(&quot;con&quot;)
response.write &quot;did not open new session&quot;
End If

If err.number <> 0 Then
err_count = 1
Response.write(&quot;Database Errors Occured&quot; & &quot;<P>&quot;)
Response.write(&quot;Error #&quot; & err.number & &quot;<P>&quot;)
Response.write(&quot;Error desc. -> &quot; & err.description & &quot;<P>&quot;)
End If

openDB = con
End function

'function call
err.clear
set con = OpenDB()
response.write err.number
response.write err.description

If Not (IsObject(con)) Then
response.write (&quot;failed to open database&quot;)
response.end
end if

and this is the exact message I get (returns added for easier reading)

opening new connection
opened new session successfully
424Object required
failed to open database
 
ok guys, I found the solution the

openDB = con

needed to be

set openDB = con

then it worked for that pages, now I have to figure out how to fix the other pages
 
AHA! it wast to obvious to notice :).
we was using Set all along to initialize objects but not that one :).

Anyway, my advice is to try to keep the connection objects as little as you can get and dont store them in session.
It's still the best way to recreate and open the connection for each page then store into session. That way you would keep 1 connection blocked as long as the user stays on the domain and if you gonna use access database then you can have more then 10 users same time on the server.

________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top