[Resolved] Annoying error trying to make functions from Database Connection code..
Here is my "standard code for connecting to an DB, and get something from an Table/Column and then close" divided into three funcions.(big code below)
The thingy is that im gonna use these three seperate functions a lot, and i dont really wanna write all this code in each page im gonna use it in.
I really wanna be able to write this instead of the big code below:
EasyCode:
VB Code:
<!--#include file="FUNCTIONS.ASP"-->
<%
ConnectDB()
DoSomething(ToMyVariable) 'and process an SQL variable here ofcourse
CloseDB()
%>
But right now i get errors like:
ADODB.Recordset (0x800A0BB9)
/FUNCTIONS.ASP, linje 55
So here is my code, and my question, what is wrong???
BIG CODE: (Functions.asp)
VB Code:
<%@ LANGUAGE="VBSCRIPT" %>
<% Function ConnectDB()
Dim Connection 'Here we declare our variable that will hold our new object
Set Connection=Server.CreateObject("ADODB.Connection")
Dim sConnString 'Here we declare our variable that will hold the connection string
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("DB/database.mdb")
Connection.Open sConnString
End Function %>
<% Function DoSomething()
Dim Recordset 'Here we declare our variable that will hold our new object
Set Recordset=Server.CreateObject("ADODB.Recordset")
Dim SQL 'Here we declare our variable that will hold the SQL statement
SQL="SELECT * FROM Produkter"
Recordset.Open SQL, Connection
Do While NOT Recordset.Eof 'i.e. carry on looping through while there are records
Response.write Recordset("Varenavn")
Response.write "<br>" 'include a line break
Recordset.MoveNext 'move on to the next record
Loop
End Function %>
<% Function CloseDB()
Recordset.Close
Set Recordset=Nothing
Connection.Close
Set Connection=Nothing
End Function %>
Here: same code, used to connect to a DB, then ask it stuff, then close...
This is the exact same code as above, only though it is NOT cut into FUNCTIONS.. but i need it to be in functions.. Please help..
VB Code:
<%
Dim Connection 'Here we declare our variable that will hold our new object
Set Connection=Server.CreateObject("ADODB.Connection")
Dim sConnString 'Here we declare our variable that will hold the connection string
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("DB/database.mdb")
Connection.Open sConnString
Dim Recordset 'Here we declare our variable that will hold our new object
Set Recordset=Server.CreateObject("ADODB.Recordset")
Dim SQL 'Here we declare our variable that will hold the SQL statement
SQL="SELECT * FROM Produkter"
Recordset.Open SQL, Connection
Do While NOT Recordset.Eof 'i.e. carry on looping through while there are records
Response.write Recordset("Varenavn")
Response.write "<br>" 'include a line break
Recordset.MoveNext 'move on to the next record
Loop
Recordset.Close
Set Recordset=Nothing
Connection.Close
Set Connection=Nothing
%>
Re: Annoying error when trying to make functions from standard Database Connection code..
Anyone? anyone who knows how to make any kind of FUNCTION that can connect to a DB, ask it stuff, and disconnect ?!?!
Re: Annoying error when trying to make functions from standard Database Connection code..
Number one rule: NEVER use reserved keywords or class names for your variable names. Change your connection and Recordset variables to a different name and try it again.
-tg
Re: Annoying error when trying to make functions from standard Database Connection code..
Well, you're right on the money, techgnome..(what money i'd give for your knowlegde!!)
But it yells another error at me on the line below, marked with <<-- Error!!
Serverobject, ASP 0177 (0x800401F3)
Invalid Classstring.
/test.asp, line 2
So what can be wrong now?
VB Code:
<%
Dim HenteVerdi 'Here we declare our variable that will hold our new object
Set HenteVerdi=Server.CreateObject("ADODB.HenteVerdi") '<<-- Error here!!
Dim SQL 'Here we declare our variable that will hold the SQL statement
SQL="SELECT * FROM Produkter"
HenteVerdi.Open SQL, Kobling
Do While NOT HenteVerdi.Eof 'i.e. carry on looping through while there are records
Response.write HenteVerdi("Varenavn")
Response.write "<br>" 'include a line break
HenteVerdi.MoveNext 'move on to the next record
Loop
%>
Re: Annoying error when trying to make functions from standard Database Connection co
Agreed, however I think the actual issue here is the scope of those variables.
They need to be available to all the functions, so can't be declared inside any of them (try declaring them above the first function).
edit: too slow!
You need to change the text inside the CreateObject back to RecordSet
1 Attachment(s)
well.. afterall.. after coding the previous example where each function had a file...
I easily now managed to create an FUNCTIONS.ASP
So now you can call functions like CONNECt and DISCONNECT from the FUNCTIONS.ASP file.. a real relief.. when it comes to not having to rewrite code each time you have to use it in another AP file..
Hehe, so now i put ConneciIT() instead og that long procedure we normally do.. just remember to INCLUDE the functions.asp in all files whre you need those functions..
I upload the finished result here, for everyone to enjoy!!!