using recordset in classic asp - asp-classic

I am trying to fetch data from database using recordset with following code in classic ASP .I am using windows 7 32 bit OS:
<%
dim con,rs
set con=Server.Createobject("ADODB.Connection")
con.Provider="Microsoft.Jet.OLEDB.4.0"
con.Open "c:\inetpub\wwwroot\New folder\123.mdb"
rs.Open "select * from student",con
for each x in rs.fields
response.write(x.name)
response.write("=")
response.write(x.value)
next
rs.Close
con.Close
%>
error is:
Microsoft VBScript runtime error '800a01a8'
Object required: ''
/New folder/recordset.asp, line 7

The word "rs" is not reserved in VBScript, you must create a recordset yourself.
Change the line:
rs.Open "select * from student",con
To this instead:
Set rs = con.Execute("select * from student")
You also iterate only the first record, to iterate all records change the code to:
Do Until rs.EOF
for each x in rs.fields
response.write(x.name)
response.write("=")
response.write(x.value)
response.write(", ")
next
response.write("<br />")
rs.MoveNext
Loop

Related

How can I load access data if this data has length=0?

I am loading data from access db but i have a problem with columns that don´t have lenght.
Here is asp code:
sql= "select * from clientes where usuario_clientes="+id_usuario
rs.Open sql, oConn
while not rs.EOF
nombre=rs.Fields("Nombre_clientes")
dni=rs.Fields("dni_clientes")
mail=rs.Fields("mail_clientes")
domicilio=rs.Fields("direccion_clientes")
cp=CStr(rs.Fields("cp_clientes"))
poblacion=rs.Fields("poblacion_clientes")
movil=rs.Fields("movil_clientes")
if not rs.Fields("observaciones_clientes") then
observaciones=" "
else
observaciones=rs.Fields("observaciones_clientes")
end if
if not rs.Fields("telefono_clientes") then
telefono=""
else
telefono=rs.Fields("telefono_clientes")
end if
fila="<tr><td>"+nombre+"</td><td>"+dni+"</td><td>"+mail+"</td><td>"+domicilio+"</td><td>"+cp+"</td><td>"+poblacion+"</td><td>"+telefono+"</td><td>"+movil+"</td><td>"+observaciones+"</td></tr>"
response.Write(fila)
rs.MoveNext
wend
I tried to show lenght 0 string if this data doesn´t exist but it not works.
I am not sure I understand, but MS Access is usually set-up to not allow zero-length strings, so these fields are likely to be null. Perhaps:
if IsNull(rs.Fields("telefono_clientes")) then
telefono="--"
else

Insert record into db2 table using ASP and return result

I'am trying to insert record into db2 table and getting a result with response.write.
My response is Insert OK, but the record isn't inserted in my table. Here's the code I use:
<%
dim connection
dim sSQL, sConnString
sSQL="INSERT INTO db.tablename (LOGON,CREATION_DATE,CREATOR) VALUES ('logonTest','2012- 06-20-12.00.00.000000','blabla')"
sConnString="data source=blabla;user id=jhonsonjack;password=mypassword"
Set connection = Server.CreateObject("ADODB.Connection")
connection.Open(sConnString)
On Error Resume Next
connection.execute(sSQL)
if Err.number<>0 or connection.Errors.Count <> 0 Then
response.write "Insert Failed"
else
response.write "Insert OK"
end if
On Error Goto 0
Connection.Close
Set Connection = Nothing
%>
Any idea on what's wrong in it?
As an update, my insert is now almost working fine. I say almost, cause i try to insert 4 records in my table, but this code only process the two firsts "insert into" and I don't understand why.
<%
dim connection
dim sSQL
dim sConnString
sSQL="INSERT INTO XXXXX.XXXXX(LOGON,CREATION_DATE,CREATOR,ACCOUNT_TYPE,TO_DISABLE,APPLICATION_NAME,ACCOUNT_MODEL,DESCRIPTION,OWNER,FAC_SECURITY,CUPID,REG_FORM,CFIRM_DATE,TO_DELETE,DOCLINK) VALUES ('XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','4','XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','','XXXXX');INSERT INTO XXXXX.XXXXX (LOGON,CREATION_DATE,CREATOR,ACCOUNT_TYPE,TO_DISABLE,APPLICATION_NAME,ACCOUNT_MODEL,DESCRIPTION,OWNER,FAC_SECURITY,CUPID,REG_FORM,CFIRM_DATE,TO_DELETE,DOCLINK) VALUES ('XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','4','XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','','XXXXX');INSERT INTO XXXXX.XXXXX (LOGON,CREATION_DATE,CREATOR,ACCOUNT_TYPE,TO_DISABLE,APPLICATION_NAME,ACCOUNT_MODEL,DESCRIPTION,OWNER,FAC_SECURITY,CUPID,REG_FORM,CFIRM_DATE,TO_DELETE,DOCLINK) VALUES ('XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','4','XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','','XXXXX');INSERT INTO XXXXX.XXXXX (LOGON,CREATION_DATE,CREATOR,ACCOUNT_TYPE,TO_DISABLE,APPLICATION_NAME,ACCOUNT_MODEL,DESCRIPTION,OWNER,FAC_SECURITY,CUPID,REG_FORM,CFIRM_DATE,TO_DELETE,DOCLINK) VALUES ('XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','4','XXXXX','XXXXX','XXXXX','XXXXX','0','XXXXX','','XXXXX');"
sConnString="data source=XXXXX;user id=XXXXX;password=XXXXXXXXXX"
Set connection = Server.CreateObject("ADODB.Connection")
connection.Open(sConnString)
set rs = connection.execute(sSQL)
if Err.number<>0 or connection.Errors.Count <> 0 Then
response.write "Insert Failed"
else
response.write "Insert OK" %><br><% response.write sSQL
end if
Connection.Close
%>
Any idea on what's wrong in there ?
You can check if any data is inserted into the table with a query like:
SELECT * FROM FINALE TABLE
(INSERT INTO...)

asp question ADODB.Recordset and calling a procedure

I have to make an quick update on a legacy asp page never really having done anything with classical asp want to check if this is valid.
Can I do this:
set conn = server.CreateObject("ADODB.Connection")
conn.Open ("connection string info")
Set rsIdent = server.CreateObject("ADODB.Recordset")
SQL = "EXEC procedureName #sParam = '" & someVariable & "'"
rsIdent.Open SQL, conn
iSome_ID = rsIdent.Fields("ident_value")
rsIdent.Close()
Set rsIdent = nothing
Where procedureName is a stored procedure that accepts a paramter, does some processing and returns a single record in a column called "ident_value"?
#Curtis: See How to call SQL Server stored procedures from ASP.

Multiple Tables Linked By ID

I have to write an ASP page that has connection to 1 database and then queries two tables one which has the header detail in and then the second which has the order lines in, each table has a ORDER_NUMNER.
These Tables contain a sales orders which I need to print out into an HTML page any help on this would be great as ASP is not my main language.
In general:
Instantiate and open your database connection: (see www.connectionstrings.com for more information)
dim conn
conn.open "your connection string goes here"
Open a recordset for the master table and detail table:
dim rst
rst.open "select * from tblMaster left join tblDetail on tblMaster.ORDER_NUMBER = tblDetail.ORDER_NUMBER where ORDER_NUMBER = 4",conn,1,3
Exit out of there are not records
if rst.eof then
rst.close
conn.close
Response.end
end if
Print header info (for fields order_date, order_number, and order_company:
response.write "Company: " & rst.fields("order_company") & "<br>"
response.write "Date: " & rst.fields("order_date") & "<br>"
response.write "Order Number: " & rst.fields("order_number") & "<br>"
Loop through records, reading all records from detail table: (for fields item_desc, item_qty, item_cost)
while not rst.eof
response.write "Item: " & rst.fields("item_desc") & "<br>"
response.write "Qty: " & rst.fields("item_qty") & "<br>"
response.write "Cost: " & rst.fields("item_cost") & "<br>"
rst.MoveNext
wend
Close the recordset
rst.close
Close the connection
conn.close
<%
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Mode = 3
objConn.Open "driver definition and connection string"
SQLStrJ = "SELECT * FROM table1 t1 JOIN table2 t2 ON t1.ORDER_NUMBER=t2.ORDERNUMBER;"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open SQLStrJ, objConn, 1, 3
Do Until objRS.EOF = True %>
<html><%=objRs("field")%></html>
<%
objRs.MoveNext
Loop
%>
You'll see in the loop an example of dropping field data into HTML. You will probably want to put the connection and recordset definition in an includable function that you can just pass a SQL string into since you'll be using it a lot. And don't forget to close the connection when you're done. Good luck.. classic ASP is a bit of a mess.

How to get the insert ID from this ADODB.Recordset?

I'm trying to avoid using straight SQL queries in my web app. I looked around and have come to the conclusion that ADO Recordsets would be the best or at least safest tool for the job. I need to insert records into a database table. Unfortunately I'm at a loss as to how to get the identity value for the record which was just inserted. Here's a reduction of what I've got now:
<%
dim insertID, rs
set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "my_table_name", conn, adOpenForwardOnly, adLockOptimistic
rs.AddNew()
Call m_map_values_to_rs(rs)
rs.Update()
insertID = rs("id")
rs.Close()
rs = Nothing
%>
The code I have is successfully inserting the record, but I can't for the life of me figure out how to get the id field of the Recordset to update after the insert. How can I get the identity column value back from this Recordset?
UPDATE - Here's the solution with regard to the code above.
I had to change the cursor type to adOpenKeyset instead of adOpenForwardOnly. After I did this the record is automatically updated with the "auto number" field's new value after the insert. However it is not what you think it is. The value of rs("id") doesn't become an integer or even a variant. It becomes some sort of Automation type and cannot be evaluated as a number. Nor can CInt() be used directly on that type for some reason. So what you must do is to convert the value to a string and then convert it to an Int. Here's how I managed that:
insertID = CInt( rs("id") & "" )
Thanks to Dee for their answer. It helped immensely.
This article explains the means of getting identity value with example code.
The relevant code snippet is:
<%
fakeValue = 5
set conn = CreateObject("ADODB.Connection")
conn.open "<conn string>"
sql = "INSERT someTable(IntColumn) values(" & fakeValue & ")" & _
VBCrLf & " SELECT ##IDENTITY"
set rs = conn.execute(sql)
response.write "New ID was " & rs(0)
rs.close: set rs = nothing
conn.close: set conn = nothing
%>

Resources