Results 1 to 8 of 8

Thread: [Resolved] Annoying error trying to make functions from Database Connection code..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Resolved [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:
    1. <!--#include file="FUNCTIONS.ASP"-->
    2. <%
    3. ConnectDB()
    4. DoSomething(ToMyVariable) 'and process an SQL variable here ofcourse
    5. CloseDB()
    6. %>


    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:
    1. <%@ LANGUAGE="VBSCRIPT" %>
    2.  
    3. <% Function ConnectDB()
    4. Dim Connection    'Here we declare our variable that will hold our new object
    5. Set Connection=Server.CreateObject("ADODB.Connection")
    6.  
    7. Dim sConnString  'Here we declare our variable that will hold the connection string
    8. sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
    9. "Data Source=" & Server.MapPath("DB/database.mdb")
    10.  
    11. Connection.Open sConnString
    12. End Function %>
    13.  
    14.  
    15.  
    16.  
    17. <% Function DoSomething()
    18. Dim Recordset    'Here we declare our variable that will hold our new object
    19. Set Recordset=Server.CreateObject("ADODB.Recordset")
    20.  
    21. Dim SQL    'Here we declare our variable that will hold the SQL statement
    22. SQL="SELECT * FROM Produkter"
    23.  
    24. Recordset.Open SQL, Connection
    25.  
    26. Do While NOT Recordset.Eof   'i.e. carry on looping through while there are records
    27. Response.write Recordset("Varenavn")
    28. Response.write "<br>"    'include a line break
    29. Recordset.MoveNext     'move on to the next record
    30. Loop
    31. End Function %>
    32.  
    33.  
    34.  
    35.  
    36. <% Function CloseDB()
    37. Recordset.Close
    38. Set Recordset=Nothing
    39. Connection.Close
    40. Set Connection=Nothing
    41. End Function %>
    Last edited by alexdata; Dec 16th, 2005 at 07:59 PM. Reason: Resolved the issue!!
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Question 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:
    1. <%
    2. Dim Connection    'Here we declare our variable that will hold our new object
    3. Set Connection=Server.CreateObject("ADODB.Connection")
    4.  
    5. Dim sConnString  'Here we declare our variable that will hold the connection string
    6. sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
    7. "Data Source=" & Server.MapPath("DB/database.mdb")
    8.  
    9. Connection.Open sConnString
    10.  
    11. Dim Recordset    'Here we declare our variable that will hold our new object
    12. Set Recordset=Server.CreateObject("ADODB.Recordset")
    13.  
    14. Dim SQL    'Here we declare our variable that will hold the SQL statement
    15. SQL="SELECT * FROM Produkter"
    16.  
    17. Recordset.Open SQL, Connection
    18.  
    19. Do While NOT Recordset.Eof   'i.e. carry on looping through while there are records
    20. Response.write Recordset("Varenavn")
    21. Response.write "<br>"    'include a line break
    22. Recordset.MoveNext     'move on to the next record
    23. Loop
    24.  
    25. Recordset.Close
    26. Set Recordset=Nothing
    27. Connection.Close
    28. Set Connection=Nothing
    29. %>
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    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 ?!?!
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    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:
    1. <%
    2. Dim HenteVerdi    'Here we declare our variable that will hold our new object
    3. Set HenteVerdi=Server.CreateObject("ADODB.HenteVerdi") '<<-- Error here!!
    4.  
    5. Dim SQL    'Here we declare our variable that will hold the SQL statement
    6. SQL="SELECT * FROM Produkter"
    7.  
    8. HenteVerdi.Open SQL, Kobling
    9.  
    10. Do While NOT HenteVerdi.Eof   'i.e. carry on looping through while there are records
    11. Response.write HenteVerdi("Varenavn")
    12. Response.write "<br>"    'include a line break
    13. HenteVerdi.MoveNext     'move on to the next record
    14. Loop
    15. %>
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Talking Partly solved..

    Well.. after lots of hustle, i managed to make the CONNECT, DISCONNECT and the ASK the DB into three files.. and the Three files is called from the fourth.. in this EX: index.asp

    So now i can call these "functions" (files) from any file whom needs it..

    Well.. now, i'll try to put this into ONE file, an FUNCTIONS.ASP
    but for now, i'll upload this, so others can look at it, and hopefully use it..
    Attached Files Attached Files
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Smile 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!!!
    Attached Files Attached Files
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width