Having issues connecting Access database with ASP/ADO - asp-classic

I am trying to connect to an access database with ADO. Below is my script. The access database is called ADOTesting1.mdb. I'm thinking it has to do with the path. This is where the file is located.
C:\inetpub\wwwroot\MyWeb
<%
Dim conn
Dim rs
Dim rdsql
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/inetpub/wwwroot/MyWeb/ADOTesting1.mdb"
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
for each x in rs.fields
response.write(x.name)
response.write(" = ")
response.write(x.value)
next
%>

You will have to use Server.MapPath to map the physical path to the website path.
Instead of:
conn.Open "c:/inetpub/wwwroot/MyWeb/ADOTesting1.mdb"
Try:
conn.Open Server.MapPath("/MyWeb/ADOTesting1.mdb")
or whatever the relative path is to your web root "/".

Related

Parameterizing sql in asp

I'm not terribly familiar with asp, but I have one project with a page of it.
<%
'declare the variables
Dim Connection
Dim ConnString
Dim Recordset
Dim SQL
Dim userName
userName = Request.Form("userName")
'define the connection string, specify database driver
ConnString="JJJJJ"//connection information
'declare the SQL statement that will query the database
SQL = "SELECT info AS Lunch, "
SQL = SQL & "FROM dbo.AgentActivityLog WHERE UserId = '" & userName & "'
'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
'Open the connection to the database
Connection.Open ConnString
'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection
'first of all determine whether there are any records
If Recordset.EOF Then
Response.Write("No records returned.")
Else
'process record
Next
Recordset.MoveNext
Loop
End If
'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>
How do I parameterize the username? I have no way to debug, no knowledge of how this language works, so whatever I tried failed and I have no idea why.
I would suggest you look at the Answer on following link as it will fill in the answers you seek:
VBA, ADO.Connection and query parameters
From the code provided critical missing piece is the ADODB.Command for the SQL to actually be run and to add the parameter the query you would :
Set Param = Command.CreateParameter("#userid",adVarChar,adParamInput)
Param.Value = userName
Command.Paramters.Append Param

Using Excel 2016 as database in ASP Classic

Does anyone know how I can display the content of an Excel 2016 file in a webpage?
I cannot figure out how to open a connection from the webpage nor which drivers I might need to install in order to open the ODBC or DSN connection.
Can anyone help me?
I got it working after using a couple of hours on it.
Heres how I read the data out
<%
Dim Conn, Sql, rs
Set Conn = Server.createobject("ADODB.Connection")
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\inetpub\wwwroot\test\test.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
' Ark1 is danish for Sheet1
Sql = "select * from [Ark1$]"
set rs = Conn.Execute(Sql)
do while not rs.eof or rs.bof
response.write rs("test") & "<br>" & vbCrLf
rs.movenext
loop
Conn.CLose
Set Conn = nothing
%>
Hope this might help others.

Save ASP classic form into a specific column in excel

I'd to know if it's possible to have my asp classic form saved into a excel file in a column after submit?
Thank you all.
use the Microsoft.Jet.OLEDB Driver to access the excel sheet like so:
dim conn : set conn = server.createObject("ADODB.Connection")
dim rs : set rs = server.createObject("adodb.recordset")
dim sql
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &_
"myExcelFile.xls;" &_
"Extended Properties=""Excel 8.0;HDR=YES;"""
then you yould use just sql to insert your data...
the possible connectionstrings for excel are listed here
THE BELOW CODE IS TO INSERT INTO AN EXISTING EXCEL FILE. THIS IS WHAT YOU NEED.
<%
Option Explicit
' OPEN DATABASE
dim objConn,strConnection,objRS,strQuery
'Set objConn = New ADODB.Connection
set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("TEST.xls") & "; Extended Properties=Excel 8.0;"
objConn.Open strConnection
'Set objRS = New ADODB.Recordset
set objRS = Server.CreateObject("ADODB.Recordset")
set objRS.ActiveConnection = objConn
' This is to Select A1:A1 and open the recordset
strQuery = "SELECT * FROM A1:A1"
objRS.Open strQuery
' This is to insert into A1:A1 a value that says: testttest
strQuery = "insert into [A1:A1] values('testttest')"
' Close and destroy the Connection object.
objConn.Execute strQuery
objConn.Close
Set objRS=Nothing
Set objConn=Nothing
%>
TO UPDATE A SPECIFIC COLUMN
You can do this: See here: http://bytes.com/topic/asp-classic/answers/620074-update-existing-excel-file-using-asp-urgent
and also here: Update Excel Sheet (in Classic ASP/Vbscript)

Syntax error (missing operator) in query expression

I know it is a common error, but I still can't solve it myself.
What I am trying to do is I have a SELECT item called status that allow the user to choose their employment status, I want to simply get the result and update the user_table(access file) status cell.
Any reply will be greatly appreciated!
The Code is below:
<!--#include file="../conn/conn.asp"-->
<%
id=request.QueryString("id")
status=request.Form("status")
sql="select * from user_table where id="&id
set rs=conn.execute(sql)
sql="update user_table set Status='"+status+"' where id="&id
'response.Write sql
conn.execute(sql)
conn.close
response.Write "<script>alert('Change Sucessful!');</script>"
set conn=nothing
response.end()
%>
I think you may be having a problem with conn.execute(sql) as well as response.end()
To fix it, you need to do either:
conn.execute sql
or
Call conn.execute(sql)
But, yeah, you should follow other comments posted as your technique has security issues. You should consider changing it to use parameters:
<!--#include file="../conn/conn.asp"-->
<%
id = request.QueryString("id")
status = request.Form("status")
sql = "select * from user_table where id = #id"
Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = sql
Set cmd.ActiveConnection = conn
cmd.Prepared = True
cmd.Parameters.Refresh
cmd.Parameters("#id") = id
Set rs = cmd.Execute
Set rs = nothing
Set cmd = nothing
sql = "update user_table set status = #status where id = #id"
Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = sql
Set cmd.ActiveConnection = conn
cmd.Prepared = True
cmd.Parameters.Refresh
cmd.Parameters("#status") = status
cmd.Parameters("#id") = id
Set rs = cmd.Execute
Set rs = nothing
Set cmd = nothing
response.Write "<script>alert('Change Sucessful!');</script>"
Set conn = nothing
response.end
%>
I'm guessing conn.asp leaves conn open? otherwise you need to open it. Also, what shows when you uncomment the response.write sql line?
And, you are definitely opening yourself to hackers. You need to 'clean' anything that comes from a request.form or request.querystring (with at the very least, a replace(..., "'", "''"), or much better, use stored procedures instead of straight sql

Classic ASP Connection

In my asp project,i have two databases in MS Access.Below code is working fine.But I need to add one more database with another dsn name.I have created dsn name efg and done the ODBC Connection for new databse in the control panel->Administrative tools.How can add the newly added dsn to the below code.
<%
session("connectionstring") = "dsn=abc"
set objconn = server.CreateObject("adodb.connection")
objconn.open session("connectionstring")
%>
Just create a second connection for the second dsn:
<%
' First dsn/connection
session("connectionstring") = "dsn=abc"
set objconn = server.CreateObject("adodb.connection")
objconn.open session("connectionstring")
' Second dsn/connection
session("connectionstring2") = "dsn=newDSN"
set objconn2 = server.CreateObject("adodb.connection")
objconn2.open session("connectionstring2")
%>

Resources