How to add records using ASP and ADO

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 16
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 ADO 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 ADO to take data from a html form and submit to an ASP page that processes the data using ADO.  A good book on ADO is by Wrox Press called ADO 2.0 Programmer's reference.)     Below is the code I used in this example, go ahead and try entering something in.   

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

<html><head>
<title>ADO add record example</title>
</head>
<body>
<h3 align="center">ADO example</h3>
<td width="424"><form method="post" name="form1" action="addrecordado2.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 strID
dim strconn

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

'Create the Connection object
set conn = server.createobject("adodb.connection")
conn.open strconn

'Create the recordset object
set rs = server.createobject("adodb.recordset")
'This statement opens the table so we can add a record notice the addnew
'The 2, 2 is how the table is opened there are many ways it can be opened
rs.open "tblAdoAdd", conn, 2, 2

'Use the addnew method of the recordset object to add a record
rs.addnew
'Set the table column = to my input text box from my form
rs("FirstName") = request("FirstName")
rs("LastName") = request("LastName")
rs("FavoriteColor") = request("FavoriteColor")
rs.update
'I do a movelast here to get the ID that is automatically generated
'I also set the value to a local variable so I can write out to the database
rs.movelast
strID = rs("ID")

%>

<html>
<head>
<title>sign on summary</title>
</head>
<body>
<TABLE BORDER=1>

Your Record # is:<% = strID %>
Your First Name is <% = request("FirstName") %>
Your Last Name is <% = request("LastName") %>
Your Favorite Color is <% = request("Favoritecolor") %>

</body></html>
<%

'Always! Always set your objects to nothing.  This clears them out of servers memory
'Your network admins will like this

set rs= nothing
set conn = nothing
%>

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 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials