Results 1 to 8 of 8

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

Threaded View

  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
    ***************

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