简体   繁体   中英

ASP connecting to SQL Server database

<%
'declare the variables
Dim Recordset
Dim sql
dim Conn
Dim name1,email1,phone1,company1,title1
name1 = request.form("TxtName")
email1 = request.form("TxtEmail")
phone1 = request.form("TxtPhone")
company1 = request.form("TxtCompany")
title1 = request.form("TxtJob")

'create an instance of the ADO connection and recordset objects
Set Conn= Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")


'open the connection to the database
Conn.ConnectionString = "DSN=blah;User Id=...;Password=...;Database=...."


'Open the recordset object executing the SQL statement and return records
Recordset.Open 
Conn.open

sql="INSERT INTO register (Name, email, phonenumber, company, title)"
sql=sql & "values ('"& name1 &"','"& email1 &"','"& phone1 &"','"& company1 &"','"& title1 &"')"


Conn.Execute(sql)
Conn.Close()

%>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Sample Registration Page</title>
</head>
<body>
    <form name"form" action="">
        <table>
            <tr>
                <td>Full Name:</td>
                <td>
                    <input name="TxtName" id="TxtName" type="text" />
                </td>
            </tr>
            <tr>
                <td>Email:</td>
                <td>
                    <input name="TxtEmail" id="TxtEmail" type="text" />
                </td>
            </tr>
            <tr>
                <td>Phone:</td>
                <td>
                    <input name="TxtPhone" id="TxTPhone" type="text" />
                </td>
            </tr>
            <tr>
                <td>Company:</td>
                <td>
                    <input name="TxtCompany" id="TxtCompany" type="text" />
                </td>
            </tr>
            <tr>
                <td>Job Title:</td>
                <td>
<input name="TxtJob" id="TxtJob" type="text" />
                </td>
        </table>
   <input name="button" ID="Button1" value="submit" type="Submit" />
    </form>
</body>
</html>

I get an error 500 message when I run this page, I have no idea where my mistake is.

I also did make the DSN connection with name called blah to my SQL Server.

I ran the ASP part alone and it works, however the database section is where my problem lies. I really appreciate any help since I'm relatively new to this.

First, you should activate the friendly display of errors on your web server in order to know exactly what and where the error is instead of the generic, say nothing Error 500 that you are getting at this moment.

Second, in the meantime, add a couple of Response.write followed by Response.Flush to see what's going and where; for example to display the result of the building of the sql string:

sql = ...
response.write sql & "<br>"
response.flush

Second, you try to open a Recordset with no associated Command object or sql query string; you cannot do that and in fact, you don't need any Recordset here because your have an Insert query, not a Select query.

Finally, using a DSN with ADODB is a bad idea. DSN are for ODBC and by using ODBC under ADODB, you are adding an old, useless layer to your data connection. You should instead use the latest native OLEDB provider for your SQL-Server. Search the web for Connection String and you will get a few web sites with full details on the available providers along with their connection strings.

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Sample Registration Page</title>
</head>
<body>
<form action="registration.asp" method="POST" name="form1">
<table>
<tr>
<td>Full Name:</td>
<td><input name="TxtName" id="TxtName" type="text" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="TxtEmail" id="TxtEmail" type="text" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input name="TxtPhone" id="TxTPhone" type="text" /></td>
</tr>
<tr>
<td>Company:</td>
<td><input name="TxtCompany" id="TxtCompany" type="text" /></td>
</tr>
<tr><td>Job Title:</td>
<td><input name="TxtJob" id="TxtJob" type="text" /></td>
</table>
<input name="button" ID="Button1" value="submit" type="Submit" />
</form>
</body>
</html>

<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" then

Dim Conn, Rs, ConnString, SQL_Insert
Dim name1,email1,phone1,company1,title1

name1 = request.form("TxtName")
email1 = request.form("TxtEmail")
phone1 = request.form("TxtPhone")
company1 = request.form("TxtCompany")
title1 = request.form("TxtJob")

Set Conn= Server.CreateObject("ADODB.Connection")
ConnString = "DSN=blah;User Id=...;Password=...;Database=...."
Conn.Open ConnString

SQL_Insert="INSERT INTO register (Name, email, phonenumber, company, title)" & _
           "values ('"& name1 &"','"& email1 &"','"& phone1 &"','"& company1 &"','"& title1 &"');"

Conn.Execute SQL_Insert
Conn.Close
Set Conn = Nothing
Set SQL_Insert = Nothing

End If
%>

As you are not retrieving any data from database there is no need of RecordSet.

copy and paste this code in a file & name it "registration.asp"

Don't forget to replace the connection string with the actual one

for tutotrial visit www.w3schools.com

hope this is helpful

A space char seems to be missing on the SQL.

sql = "INSERT INTO register (...)SPACE-MISSING-HERE"
sql = sql & "values (...)"

Conn.Execute(sql)
Conn.Close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM