Access Connection String From File Generated by Server.Execute - vbscript

I have a file header.asp that has an server side include head.asp. Inside head.asp my connection to my database is created and is accessible from the header.asp file and pages that use the header.asp file as an server side include (in this case my core.asp file). Inside core.asp I have the following:
if request.querystring("page") = "" then
response.write("<p>No data to load</p>")
else
page = request.querystring("page")
set fso = createobject("scripting.filesystemobject")
FileName = "../Pages/" & page & ".asp"
if fso.FileExists (server.mappath(FileName)) then
Server.Execute(FileName)
else
response.write("<p>Could not validate page. Try again.</p>")
end if
end if
This executes just fine and the proper page renders inside the core.asp file. My issue, is that the connection string (from head.asp) is not available to the file being called from Server.Execute. Thus, I cannot run database queries, etc. on this page unless I were to instantiate the object anew. Is there any way to use the object created?
If I have not explained this properly, I will expound as I am able given my intermediate experience level.

As a follow up to answer #1, if I've followed the logic correctly, I'm attempting to create the connection object at the application level (global.asa):
Sub Application_OnStart()
dim oConn, connectstr
set oConn = Server.CreateObject("ADODB.Connection")
connectstr = "Driver={MySQL ODBC 3.51 Driver};SERVER=theServer; DATABASE=theDatabase; UID=theUserID; PWD=thePassword"
oConn.ConnectionTimeout = 5
oConn.Open connectstr
Application("con") = oConn
end Sub
</script>
Attempting to access the object like so from my page that includes the global.asa as an IIS:
qryAdmin = "select * from table"
set rsAdmin = Application("con").execute(qryAdmin)
The results are the same. I receive the "Object required" error. Any glaring errors that could be causing this?

Assign this object to a session variable and you can access it from any page you like...
Session("MyConnectionObj")= CONN
CONN is your instantiated connection object on the page where you instantiate your connection originally. And in your executed file just call
set NewCONN=Session("MyConnectionObj")
RS.Open strSql ,NewCONN,1,1
where strSql is your SQL, RS is your recordset etc... Obviously if you calling the stored procedure, it will look slightly different but you get the point.
Edited to show that object have to be declared and set before one using it. As I see at least one "expert" did not know that.
Edited base on comments from Window Frog:
did you try it like that:
<%
if request.querystring("page") = "" then
response.write("<p>No data to load</p>")
else
page = request.querystring("page")
set fso = createobject("scripting.filesystemobject")
FileName = "../Pages/" & page & ".asp"
if fso.FileExists (server.mappath(FileName)) then%>
<!--#include file="<%=FileName %>" -->
<% else
response.write("<p>Could not validate page. Try again.</p>")
end if
end if
%>

Related

Find the name all tables in DBF database

I'm trying to find out the names of the tables.DBF, which are in a hard drive directory.
Subsequently, I need to know the names of their columns, but even the names of the tables I do not know how to find out. Standard SQL tools do not work, it's logical. The script is run on the site under IIS (win server 2008), VBScript. For the connection, I use the driver: AccessDatabaseEngine_X64.
The selection from the specific tables is working, create, delete the table - it works. Connection code:
<%#LANGUAGE="VBSCRIPT" CODEPAGE=1251%>
<%
Function OpenDBFConn(Path)
Dim Conn: Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider = Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Path & ";" & _
"Extended Properties=""DBASE IV;"";"
Set OpenDBFConn = Conn
End Function
Dim DBConn
Set DBConn = OpenDBFConn("C:\update")
Dim Lel
Set Lel = DBConn.Execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_type='BASE TABLE'")
%>
The script result:
The Microsoft Access database engine could not find the object 'INFORMATION_SCHEMA.TABLES'. Make sure the object exists and that you spell its name and the path name correctly. If 'INFORMATION_SCHEMA.TABLES' is not a local object, check your network connection or contact the server administrator.
This is logical. Here, the methods for accessing the system table will not work, because they are not. Question - how do I know the names of the tables in the 'Update' folder? Screenshot of the folders:
1) Table names. As it's already mentioned, get table names (i.e. M1072R, M10201, etc.) by using FileSystemObject:
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set fo=fs.GetFolder("C:\update")
For Each x in fo.files
If LCase(fs.GetExtensionName(x.path)) = "dbf" Then
Response.Write fs.GetBaseName(x.path) & ";"
End if
Next
2) Column names:
'Dim Lel
'Set Lel = DBConn.Execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_type='BASE TABLE'")
Set rs=Server.CreateObject("ADODB.Recordset")
rs.Open "M1072R", DBConn
'-- where M1072R is your table/dbf file
For Each f In rs.Fields
Response.Write "Column=" & f.Name & ";"
Next
Another way to get columns is to use OpenSchema
Set rs=DBConn.OpenSchema(4, Array(Null, Null, "M1072R"))
'-- where M1072R is your table/dbf file,
'-- 4 stands for adSchemaColumns
Do While Not rs.EOF
Response.Write "Column=" & rs.Fields("COLUMN_NAME").Value & ";"
rs.MoveNext
Loop

Classic asp page is throwing "internal server 500 error"

Connection file
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "abc","ID","Password"
conn.commandtimeout=120
Set RS = Server.CreateObject("ADODB.RecordSet")
rs.activeConnection = Conn
%>
Classic ASP file
<%response.buffer = true%>
<%Response.Expires = 0%>
<!-- #include file="functions.asp" -->
<% Response.Write session("RequestID")%>
<%if session("ValidLogon") <> "true" then
if request("FromEmail") = "True" then
SetSessVar()
else%>
<%response.redirect "Default.asp"
end if
end if%>
<html>
<body>
<%rs.Source = "SELECT * from tblRequests WHERE RequestID = " & request("requestID")
rs.Open
session("RequestID") = rs("requestid")
if rs("RequestType") = "O" then
response.clear
If request("Tag") = "Change" then
response.redirect "abc.asp#change"
else
response.redirect "abc.asp?From=" & request("From")
end if
else
response.clear
If request("Tag") = "Change" then
response.redirect "editinternal.asp#change"
else
response.redirect "editinternal.asp?From=" & request("From")
end if
end if
rs.close%>
</body>
</html>
I have checked the classic asp page and it looks like there is an error in syntax inside "Body" tag. I don't know anything about it.
It is giving internal server error 500.
In Connection File you are missing Set on rs.activeConnection = Conn as you are setting an object reference to the ADODB.Connection object instance not passing a connection string.
'Object instances require Set
Set rs.activeConnection = Conn
Please make sure that you configure your site to send the detailed error messages to the client
This describes how:
Show detailed errors
I would guess that your connection "abc"/"ID"/"Password" has to be a real connection. It seems that you just wrote something to see what happens. It could also be the "functions.asp" file that you are including. Does that file exist, what does it contain?
Please post back your detailed error messages, then we can help you better.
Before referencing to the recordset you should check if the recordset contains any records like:
If not rs.eof then
Session("reqestid") = rs("reqestid")
....
End If
Just want to add one trix here, try add these 2 lines right after the body tag
Response.Write "<br>For the sake of debug"
Response.Flush
If you have buffering on, this sometimes writes out the error instead of throwing out error 500. Helps me often.

Unspecified error while executing sql query vba on Oracle database

I want to import data from Oracle database to Excel using VBA. I've tried several options I found in here or in the official manual but none of them seem to work for me - I always get the same unspecified runtime error on line with
rs.Open strSQL1, con
or
Set rs = con.Execute(strSQL1)
depending on which one of these two methods I use obviously. Here is the whole code:
Sub data_distribution()
'Setting up the database connection
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL1, strInput, strCon As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=XXX)(PORT=XXX))" & _
"(CONNECT_DATA=(SERVICE_NAME=XXX))); uid=XXX; pwd=XXX;"
'--- Open the above connection string.
con.ConnectionString = strCon
con.Open
'con.Open strCon
'--- Now connection is open and you can use queries to execute them.
'--- It will be open till you close the connection
'Definition of parameter
strInput = InputBox("Insert car_id")
strSQL1 = "select * from car where car_id = " & strInput & ""
'Executing the query
'rs.activeconnection = con
'rs.Open strSQL1, con
Set rs = con.Execute(strSQL1)
rs.Open strSQL1, con
Worksheets("Data").Range("A2").CopyFromRecordset rs
'Closing connection
rs.Close
con.Close
Set con = Nothing
Set rs = Nothing
I was thinking it might be an issue of connecting to the database but if I import/query data manual, it works just fine. For example using this manual
http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/
I had to download and configure ODAC for Windows from Oracle site to make it work. I use tsnames.ora to set the path. Wasn't sure if I configured it right but it works so I guess there isn't a problem with the connection in vba either, or is it? (The variables are, of course, set to real values, not "XXX")
The query itself is correct and returns valid results from the database. Libraries necessary to use ADOBD are linked as well. (Microsoft ActiveX Data Objects 2.0 Library)
There is an existing question with the same error but it's unresolved:
Unspecified run time error while executing vba script
I'm guessing it's station/interface specific (I use Excel 2010, SQL developer).
Even though it's been some time I asked the question, here, it's still unresolved so I will answer myself.
The problem was in the car table where one of the attributes was type CLOB (CHARACTER LARGE OBJECT) with lenght over 2000 characters. Excel was unable to cope with that type and the query caused the unspecified error. If I listed all attributes but this one, it all went well.

How to add default signature and new tables in a new outlook mail by testcomplete

I am trying to send new outlook mail by testcomplete using vb scripting language .In that new mail i want to add new tables and keep default signature at bottom of mail by testcomplete. i am getting VB script run time error when i am using this code..please check the code and suggest me the correct methods i have to use to add new tables and signature
Function SendMail()
Dim objOutLook, NamespaceMAPI,objNewMail, fso, SendReceiveControls
Dim strTo,strCc ,strBcc ,strSubject, AccountName,strAttachmentPath
strSubject="test"
strTo=yyy#yy.com
strCc=XXX#XX.com
strBcc =zzz#zzz.com
strAttachmentPath="c:\text.txt"
Set objOutLook = CreateObject("Outlook.Application")
Set NamespaceMAPI = objOutLook.GetNamespace("MAPI")
Set objNewMail = objOutLook.CreateItem(olMailItem)
objOutLook.DisplayAlerts =True
objNewMail.TO = strTo
objNewMail.CC = strCc
objNewMail.BCC=strBcc
objNewMail.Subject = strSubject
objNewMail.Body = strMsg
If strAttachmentPath <> "" Then
Set fso =CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strAttachmentPath) Then
objNewMail.Attachments.Add(strAttachmentPath)
objNewMail.GetDefaultsignature() 'script run time error occured here
objNewMail.addtable(4,3)
objNewMail.display
Else
msgbox "Attachment File Does not exists"
End If
End If
objOutLook.Quit
''''''' Releasing objects '''''''
Set objOutLook =Nothing
Set objNewMail = Nothing
Set fso = Nothing
End Function
please help me.. thanks in advannce....
See if this or this helps. They are alternative methods to yours.
I prefer to use the 2nd option, the CDO method, you just need to take atention to the fact that usually this email goes to the spam inbox, you need to manually add it to your secure contacts

ADS user details - subdomain - from vbs file

I managed to get ADS users without specifying authentication details from my ADS domain(ex,mydomain.com). I used ADODB.Connection and ADODB.Command.
I also have sub-domains like test.mydomain.com. How to get user details from sub-domain, by specifying authentication details of a user belonging to test.mydomain.com .
You can query records from trusted domains by using their LDAP name as the search base. However, since the DC of the parent domain doesn't contain the information about objects in the child domain it will generate a referral. The ADODB.Command object won't automatically chase that referral, because the respective named property "Chase referrals" defaults to 0x00 (ADS_CHASE_REFERRALS_NEVER). You have to set the property to one of the following two values
ADS_CHASE_REFERRALS_SUBORDINATE (0x20)
ADS_CHASE_REFERRALS_ALWAYS (0x60)
to make your query follow the referral. Example:
base = "<LDAP://dc=test,dc=example,dc=org>"
filter = "(&(objectCategory=computer)(name=foo*))"
attr = "name,description"
scope = "subtree"
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = base & ";" & filter & ";" & attr & ";" & scope
cmd.Properties("Chase referrals") = &h60 ' <-- here
Set rs = cmd.Execute
I wrote a wrapper class (ADQuery) to encapsulate the boilerplate code for Active Directory queries (because I got fed up with writing it over and over again). With that you could simplify the above to something like this:
Set qry = New ADQuery
qry.SearchBase = "dc=test,dc=example,dc=org"
qry.Filter = "(&(objectCategory=computer)(name=foo*))"
qry.Attributes = Array("name", "description")
Set rs = qry.Execute
Either way you may still need to run the script on a DC, though.

Resources