PDA

View Full Version : adding (*#!!) to access database


leeolive
06-05-2003, 09:54 AM
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="GENERATOR" Content="Microsoft Visual Studio 6.0">

</HEAD>
<body>

<!-- #include file = "ADOVBS.INC" -->

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

<%
strname=Request.Form("name")


Response.Write strname


Set cndb = server.CreateObject("adodb.connection")
Set cmdaddtodatabase = server.CreateObject("adodb.command")

cndb.CommandTimeout = 15
cndb.CursorLocation = adUseServer
cndb.ConnectionString = "DSN=cjrcls"
on error resume next
cndb.open

if err <> 0 then
response.write "Error connecting to the database"
response.end
end if

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

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



%>


</body>
</HTML>

badboy_flash
06-08-2003, 02:45 PM
hello leeolive try putting this code after the <%@language=Vbscript%> tag

<% Option Explicit %>

try tbis first.

You may get some errors coming back saying this variable is not defined or that varibale is not defined but thats fine.

The line of code helps you to define every variable properly by Dimentioning it.

After your variables are defined if you still get an error it will be easier to see where it is coming from.

The other thing I noticed is that are you creating a stored procedure for MS access??

Just curious.

badboy_flash
06-08-2003, 02:54 PM
leeolive here is an article that explains it even more.

Dim variables (http://programmersresource.com/forum/showthread.php?s=&threadid=108)