How to update records using ASP and ADO

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
September 01, 1999
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Updating Records to a Database using ADO(ActiveX Data Objects)


 
All ASP Code is in RED
'This page doesn't use a session variable so for performance reasons I set the two directives. 
'By Setting the EnableSessionState you save the asp.dll from making a call to the MTS to check
'for Session variables used by this page

<%@ Language = "VBScript" @ ENABLESESSIONSTATE=False %>
<%
'Declare all local variables

dim conn
dim rs
dim strsql
dim strconn
'Set local variable to the connection string and open the connection
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("adoandsqladd.mdb")
set conn = server.createobject("adodb.connection")
conn.open strconn
'Set local recordset variable equal to rs
set rs = server.createobject("adodb.recordset")
'Sets the sql query that determine what records get brought back to the asp page.
'This could easily be change to a very specific set of fields or instead of using a ORDER BY statement
'Using a  WHERE statement to determine what records get selected IE(WHERE tblUPDATEADO.id=1)

strsql = "SELECT * FROM tblUpdateADO ORDER BY id"
rs.open strsql, conn
%>

<html>
<head>
<title>signon</title>
</head>
<body>
<h3 align="center">Update w/ADO example</h3>
<td width="10%"><form method="post" name="form1" action="updateado2.asp">
<table>
<%
'Writes out the records, again this could just writing out one record just take away the DO While LOOP


x = 1
rs.movefirst
Do While NOT rs.EOF
%>

<tr><td><strong>First Name</strong></td><td>
<input type="text" size="10" name="FirstName<%= x %>" value="<% = rs("FirstName")%>"></td>
<td><strong>Last Name</strong></td><td>
<input type="text" size="10" name="LastName<%= x %>" value="<% = rs("LastName")%>"></td>
</tr>
<%
x = x + 1
'Don't forget to move the recordset to the next record or your going to have an infinate loop
'Trust me this statement I always forget!

rs.movenext
Loop
%>

<td><input type="Submit" value="Submit Update Example" name"b1"> </font>
</form>
</td></tr>
</table>
</center></div>
</body></html>

Page 2 the page actually re-submits the information back to the database  Here is the code that is used to update the information on the demo.

'This page doesn't use a session variable so for performance reasons I set the two directives. 
'By Setting the EnableSessionState you save the asp.dll from making a call to the MTS to check
'for Session variables used by this page

<%@ Language = "VBScript" @ ENABLESESSIONSTATE=False %>
<%

'Declare all local variables

dim conn
dim rs
dim strID
'Set local variable to the connection string and open the connection
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("adoandsqladd.mdb")
set conn = server.createobject("adodb.connection")
conn.open strconn
'Set record set object and opens the table that will update the records
set rs = server.createobject("adodb.recordset")
rs.open "tblUpdateADO", conn, 2, 2
rs.movefirst
for x = 1 to 5
rs("FirstName") = request("FirstName" & x)
rs("LastName") = request("LastName" & x)
rs.update
rs.movenext
Next
rs.movefirst
%>

<html>
<head>
<title>Results of the Update ADO Demo</title>
</head><body>

<table BORDER="0">
<% x = 1
rs.movefirst
Do While NOT Rs.EOF
%>

<tr><td><strong>First Name</strong></td><td><% = rs("FirstName")%></td><td>
<strong>Last Name</strong></td><td><% = rs("LastName")%></td></tr>
<%
x = x + 1
rs.movenext
Loop
%>

</table>

<p><a href="updateado.asp">Click here to go back to the start page</a></p>
</body>
</html>
<%
set rs= nothing
set conn = nothing
%>
Good Luck!!
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 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials