How to add records to a database using ASP and formatted SQL

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
September 01, 1999
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

My purpose of this example is to show how to get records from an html form and submit that form to an ASP page.  This ASP page will then use SQL to enter the records into the database    When I started doing ASP, getting records from an ASP page to a database was my 1st thing I wanted to do.  I quickly discovered there was a few ways you could do that. This example uses formatted SQL and the Execute method of the Connection object to take data from a html form and submit to an ASP page that processes the data using SQL.   

1st page the Input form- This is just a standard html Input form

<html><head>
<title>SQL add record example</title>
</head>
<body>
<h3 align="center">SQL example</h3>
<td width="424"><form method="post" name="form1" action="addrecordsql2.asp">
<p><strong>First Name</strong><br>
<input type="text" size="40" name="FirstName"><br>
<strong>Last Name</strong><br>
<input type="text" size="40" name="LastName"><br>
<strong>FavoriteColor</strong><br>
<input type="text" size="40" name="FavoriteColor"><br>
<p><input type="Submit" value="Submit New Example" name"b1"> </font></p>
</form>
</td>
</tr>
</table>
</center></div>
</body></html>

2nd The ASP page that process the information

<%@ Language = "VBScript"%>
<%
'Declare all local variables
dim conn
dim rs
dim strconn
dim strsql

strsql = ""
'set connection string to local variable-I use a DSN-less connection
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("adoandsqladd.mdb")

'build the sql statement based on the input from the form
strSQL = "INSERT INTO tblSqlAdd(FirstName, LastName, FavoriteColor) Values('" & request("FirstName") & "', '" & request("LastName") & "', '" & request("Favoritecolor") & "')"

'Set connection object
set conn = server.createobject("adodb.connection")
conn.open strconn
'Use the execute method of the connection object the insert the record
conn.execute(strSQL)
conn.close
set conn = nothing
%>
<html><head>
<title>SQL Add record example</title>
</head>

<body>
<% = "Your record has been added" %>
</body>
</html>

blog comments powered by Disqus
DATABASE CODE ARTICLES

- Deployment of the MobiLink Synchronization M...
- MobiLink Synchronization Wizard in SQL Anywh...
- Finding Matching Records in Data Access Pages
- Using the AccessDataSource Control in VS 2005
- A Closer Look at ADO.NET: The Command Object
- A Closer Look at ADO.NET: The Connection Obj...
- Using ADO to Communicate with the Database, ...
- Code Snippets: Counting Records
- Constraints In Microsoft SQL Server 2000
- Multilingual entries into a DB and to be dis...
- Two combos, one textbox example
- ADO Recordset Paging
- SQL Server Database Creator - .NET Version
- Getting A List of Tables From SQL Server
- Discussion & Listserv Module by Mike Eck...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials