-1

I am using this way to connect and use the Conn object for connections

 <!-- #include file="dataconnections.asp" -->

In one of my ASP pages, I have a VBScript Onclick event that needs me to execute a stored procedure and pass the variables in the textboxes.

Right now, the Connection String is hardcoded in the ASP File itself and is not using the Conn object from the include file. Is there any proper way to execute the SP without using a new Conn, or how can I pass the current Conn from the include file to the one being used in the VBScript Onclick event?

Sid
  • 765
  • 5
  • 29
  • 57
  • Presumably your include file has a connection object defined? That should be visible from the server side code in your asp file. – Sean Lange Aug 19 '14 at 20:30
  • 1
    VBScript Onclick event/ VBScript functions doesnt recognize preprocessed asp codes, or am I missing something. – Sid Aug 19 '14 at 21:00
  • Inside your click event handle you need to execute server side code. <% Put your server code inside here %> – Sean Lange Aug 19 '14 at 21:23
  • You mean the connection string? I wanted to do something like this Set NewConn = <%=IncludeConn%> – Sid Aug 19 '14 at 21:59
  • 1
    What's the code inside of the include file look like? – Control Freak Aug 20 '14 at 03:05
  • You're not missing something @SeanLange is talking utter rot. This is Classic ASP not ASP.Net preprocessing isn't supported. – user692942 Aug 20 '14 at 09:42

1 Answers1

0

I don't know what your include dataconnections.asp contains (would have been nice if you had posted the source code in your question) but a stored procedure is best run using the ADODB.Command object which exposes the ActiveConnection property.

This allows you to either Set your ADODB.Connection object or pass your connection string and let the ADODB.Command instantitate a new ADODB.Connection using that connection string.

Here are examples of using the two options

  1. Use existing ADODB.Connection

    Dim cmd
    
    Set cmd = Server.CreateObject("ADODB.Command")
    With cmd
      'Use existing object from include file to set connection.
      Set .ActiveConnection = YourConnObject
    End With
    

    IMPORTANT: When using this method make sure the YourConnObject (ADODB.Connection) should have been opened using

    'Make sure connection has been opened.
    Call YourConnObject.Open()
    

    before assigning to the ADODB.Command object.

  2. Use connection string variable (probably defined in your include) to set the connection that will be exclusively used by the ADODB.Command object.

    Dim cmd
    
    Set cmd = Server.CreateObject("ADODB.Command")
    With cmd
      'Use connection string to instantiate a new connection to be used
      'exclusively by the ADODB.Command object.
      .ActiveConnection = YourConnectionString
    End With
    

    NOTE: Unlike option 1 calling

    'Close ADODB.Command object
    Call cmd.Close()
    

    Will instantaneously destroy the ADODB.Connection (that was assigned while setting the ActiveConnection property).


Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • Hi, my main problem is when using SP on a Function / Sub as VBSCRIPT. a scenario would be, choosing a Name from dropdown then calling the Sub/Function to execute the SP and passing the Name argument. Now, I need to Call a New Conn.Open with since the #include was already processed before. I dont know how to elaborate this properly or maybe I am overthinking things. Right now, I put the connection string to a variable then reuse the connection string. The problem on this one, it shows the connection string on VIEW SOURCE. – Sid Aug 20 '14 at 16:27
  • @Sid When you say function you mean server-side not client-side right? Your going to have to show some code, I can't keep second guessing you, [post it in your question](http://stackoverflow.com/posts/25392225/edit). – user692942 Aug 20 '14 at 16:29
  • @Sid I'd recommend you post both the code for the page and the code for the include or people are going to struggle to help. We have no idea what `dataconnections.asp` does and as you mention it in [your comment](http://stackoverflow.com/questions/25392225/asp-classic-getting-and-setting-the-conn-to-another-conn-object#comment39635562_25401753), it's important for us to know what it does. – user692942 Aug 20 '14 at 17:40