Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is this code not working? (From a site) 2

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
GB
I've grabbed this code from

But I'm having trouble with the second file. It will not write to my database. The first and second (create form and submit, and display data in table) files are fine, but it's this second...
My database (.mdb) is called TempASPLink and the table fields are firstname and lastname

<HTML>

<HEAD>
<TITLE>Text to Database</TITLE>
</HEAD>

<BODY>

<%
firstname=request.form(&quot;firstname&quot;)
lastname=request.form(&quot;lastname&quot;)
%>

<%
accessdb=&quot;TempASPLink&quot;
cn=&quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot;
cn=cn & &quot;DBQ=&quot; & server.mappath(accessdb)
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

sql = &quot;insert into testtable (lastname, firstname) &quot;

sql = sql & &quot;values ('&quot; & firstname
sql = sql & &quot;','&quot; & lastname & &quot;')&quot;

rs.Open sql, cn
%>
<font face = &quot;arial&quot; size=&quot;1&quot;>
First Name <%= firstname %>
<br>
Last Name <%= lastname %>
<br>
has been added to the db <br>
<a href = &quot;input_text_db_view.asp&quot;>View the Table</a>
</font>

</BODY>

</HTML>
 
Try something closer to this:

strConnect = &quot;...connection string...&quot;
Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
objConn.Open strConnect
strSQL = &quot;INSERT INTO tblTable1 (decValue1) VALUES (3.4)&quot;
objConn.Execute(strSQL)
objConn.Close
Set objConn = Nothing


Kris
- The only program that runs perfectly every time, is a virus
 
I'll give it a try

The problem is centred around
<%
accessdb=&quot;TempASPLink&quot;
cn=&quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot;
cn=cn & &quot;DBQ=&quot; & server.mappath(accessdb)
set rs = server.createobject(&quot;ADODB.Recordset&quot;)

SQL = &quot;insert into testtable (lname, fname) &quot;

SQL = SQL & &quot;values ('&quot; & u_lname
SQL = SQL & &quot;','&quot; & u_fname & &quot;')&quot;

rs.Open sql, cn // HERE!!!!!!!!!!!!!!!!!!!!!
%>

I believe, but I'll have a go with yours.

 
you need the ext. on the DB
accessdb=&quot;TempASPLink.mdb&quot; A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Here's where the code was from




adding .mdb doesn't work - I don't need it I reckon because the third page which displays the data doesn't use it.

I can connect to the database using the third file -

<%
accessdb=&quot;TempASPLink&quot;
cn=&quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot;
cn=cn & &quot;DBQ=&quot; & server.mappath(accessdb)
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

sql = &quot;select * from testtable&quot;

rs.Open sql, cn
%>

// do display work

rs.Open sql, cn works here, but not for inputting data.
 
I don't need a query to be present in the database for this to work do I?
 
try some debug steps.
first the problem is either in your connection string or the sql statement. So
write the SQL var to the screen and see if it actual looks the way it should
Response.Write SQL
second
dot he same with the connection string
Response.Write cn


A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
try this and see if ti works
<HTML>

<HEAD>
<TITLE>Text to Database</TITLE>
</HEAD>

<BODY>

<%
firstname=request.form(&quot;firstname&quot;)
lastname=request.form(&quot;lastname&quot;)
' test the values
' response.write firstname & &quot; &quot; & lastname
%>

<%
accessdb=&quot;TempASPLink.mdb&quot;
cn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; & Server.MapPath(&quot;accessdb&quot;) & &quot;;&quot;
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

sql = &quot;INSERT INTO testtable (lastname, firstname) &quot;
sql = sql & &quot;values ('&quot; & firstname
sql = sql & &quot;','&quot; & lastname & &quot;')&quot;
' test response.write sql
rs.Open sql, cn
%>
<font face = &quot;arial&quot; size=&quot;1&quot;>
First Name <%= firstname %>
<br>
Last Name <%= lastname %>
<br>
has been added to the db <br>
<a href = &quot;input_text_db_view.asp&quot;>View the Table</a>
</font>

</BODY>

</HTML>

take out the commented ares to test. there is no reason for you to concatinate your connection string. it's short and simple so there is no need to give it the oppurtunity for errors to occur A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
apoogies I meant this line to be
get rid of the variable accessdb

cn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; & Server.MapPath(&quot;TempASPLink.mdb&quot;) & &quot;;&quot; A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
other things to look at

is the write permmisions set to the directory the DB is placed in?

open the connection for read/write like this also
rs.Open sql, cn,3,3
A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
I've got [Microsoft][ODBC Microsoft Access Driver] Operation must use an updateable query.

I'm using your rs.Open sql, cn,3,3 code at the bottom. Testing SQL and the data they both have the expected output.
 
that leads me to believe the write permissions are incorrect or not set on the directory the DB is located.

have they been set.
how are you testing this? A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
you would be right!
I right-clicked on the file, selected EVERYONE under permissions and checked the WRITE box. I don't know if this compromises our intranet's security but this will do for now.

Here is the code at the moment - am going to try to rewrite so it doesn't use rs.Open but a connection object.

<HTML>

<HEAD>
<TITLE>Text to Database</TITLE>
</HEAD>

<BODY>

<%
firstname=request.form(&quot;u_fname&quot;)
lastname=request.form(&quot;u_lname&quot;)
' test the values
response.write firstname & &quot; &quot; & lastname
%>

<%
accessdb=&quot;TempASPLink.mdb&quot;

cn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; & Server.MapPath(&quot;TempASPLink.mdb&quot;) & &quot;;&quot;
set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

sql = &quot;INSERT INTO testtable (lname, fname) &quot;
sql = sql & &quot;values ('&quot; & firstname
sql = sql & &quot;','&quot; & lastname & &quot;')&quot;
' test response.write sql
response.write sql
rs.Open sql, cn,3,3
%>
<font face = &quot;arial&quot; size=&quot;1&quot;>
First Name <%= firstname %>
<br>
Last Name <%= lastname %>
<br>
has been added to the db <br>
<a href = &quot;input_text_db_view.asp&quot;>View the Table</a>
</font>

</BODY>

</HTML>
 
should not have to worry about your intranet's security with this. don't take me up on that one though. [lol]

glad you got it A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top