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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding to access database - what's wrong?

Status
Not open for further replies.

leeolive

Programmer
May 14, 2002
46
GB
Hi

I am new to asp. I have managed to retrieve from an access database, but can't seem to get the simplest adding to database page working. The connection is fine, but it comes up with an 'error adding to db' error message

Can anyone spot the problem?? Here's the code

Thanks! Lee

<%@ Language=VBScript %>
<%Response.Buffer=True%>
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>

</HEAD>
<body>

<!-- #include file = &quot;ADOVBS.INC&quot; -->

<%
Dim strname, strcity
Dim cndb, cmdaddtodatabase
Response.Expires=0
%>

<%
strname=Request.Form(&quot;name&quot;)


Response.Write strname


Set cndb = server.CreateObject(&quot;adodb.connection&quot;)
Set cmdaddtodatabase = server.CreateObject(&quot;adodb.command&quot;)

cndb.CommandTimeout = 15
cndb.CursorLocation = adUseServer
cndb.ConnectionString = &quot;DSN=cjrcls&quot;
on error resume next
cndb.open

if err <> 0 then
response.write &quot;Error connecting to the database&quot;
response.end
end if

With cmdaddtodatabase
.CommandTimeout = 15
set .ActiveConnection = cndb
.CommandType = adCmdText
.CommandText = &quot;INSERT INTO testtable (name) VALUES ([pname]);&quot;
.Parameters.Append(.CreateParameter (&quot;pname&quot;,adVarChar,adParamInput,50,strname))
on error resume next
.Execute

if err.number <> 0 then
Response.Write &quot;error adding to db&quot;
Response.End
end if
End with



%>


</body>
</HTML>

 
How about replacing response.write &quot;error etc....&quot;
with response.write err.description and telling us what the error is?

Rob
 
Thanks for getting back, Rob.

This is the error description

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

Doesn't seem to be passing the parameters.
 
I'm not spotting the parameter problem, but whatever it is you should be able to eliminate it by changing:
Code:
.CommandText = &quot;INSERT INTO testtable (name) VALUES ([pname]);&quot;
.Parameters.Append(.CreateParameter (&quot;pname&quot;,adVarChar,adParamInput,50,strname))
to
Code:
.CommandText = &quot;INSERT INTO testtable (name) VALUES ('&quot; & strname & &quot;');&quot;
Alternately, I don't recall if using .Parameters correctly adds single-quotes to strings, so if it doesn't you could instead change your .CommandText line to:
Code:
.CommandText = &quot;INSERT INTO testtable (name) VALUES ('[pname]');&quot;
(and keep your .Parameters line).
 
Thanks for getting back. I tried your suggestions and this seems to add to the db (we are getting somewhere!) but it is adding 'strname' and not the actual value. Must be something to do with the syntax. I've played around but still can't get it to work.

.CommandText = &quot;INSERT INTO testtable (name) VALUES ('[strname]');&quot;

Thanks, I appreciate this.
Lee
 
what Genimuse tried to say was:
strname must be a varialbe that stores the value.
strname=request(&quot;the_field&quot;)

so try this query:
INSERT INTO testtable (name) VALUES ('[&quot;&strname&&quot;]')

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top