Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]   ADO: Input Form, added w/SQL
     [next Lesson]  ADO: Tables within Databases

Add New Record with ADO

This page demonstrates the capabilities how to add a record to a database using ADO instead of SQL. The script is:

   filename=/learn/test/dbnewADO.asp

<Test Script Below>


<html><head>
<title>dbnewrec.asp</title></head>
<body bgcolor="#FFFFFF">
<% ' My ASP program that allows you to append a record %>
<form name="myauthor" action="dbnewADOrespond.asp" method="GET">
<p>Author ID: <input type="TEXT" name="id"></p>
<p> Author Name: <input type="TEXT" name="name"></p>
<p> Year Born: <input type="TEXT" name="year"></p>
<p> <input type="SUBMIT"> </p>
</form></body></html>

The form responder looks like this:

   filename=/learn/test/dbnewADOrespond.asp

<Test Script Below>


<TITLE>dbnewADO.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<!--#INCLUDE VIRTUAL="/ADOVBS.INC" -->
<!--#INCLUDE VIRTUAL="/learn/test/lib_errors.asp" -->
<%
on error resume next
auname=request.querystring("name")
auyear=request.querystring("year")
auID=request.querystring("ID")
If auid<9000 then
    auid=auid+9000
end if
conn="DSN=Student;uid=student;pwd=magic"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open "authors",Conn,adopenstatic,adlockoptimistic
RS.AddNew
'RS("AU_ID")=auid
RS("Author") = auname
RS("Year_Born")= int(auyear)
RS.Update
Call ErrorVBscriptReport("Adding Record")
Call ErrorADOReport("Adding Record",RS.activeconnection)
RS.Close
set rs=nothing
%>
</BODY>
</HTML>




Here is the include file that displays appropriate errors:

   filename=/learn/test/lib_errors.asp

<Test Script Below>


<%
SUB ErrorVBScriptReport(parm_msg)
    If  err.number=0 then
        exit sub
    end if
    pad="&nbsp;&nbsp;&nbsp;&nbsp;"
    response.write "<b>VBScript Errors Occured!<br>"
    response.write parm_msg & "</b><br>"
    response.write pad & "Error Number= #<b>" & err.number & "</b><br>"
    response.write pad & "Error Desc.= <b>" & err.description & "</b><br>"
    response.write pad & "Help Context= <b>" & err.HelpContext & "</b><br>"
    response.write pad & "Help File Path=<b>" & err.helpfile & "</b><br>"
    response.write pad & "Error Source= <b>" & err.source & "</b><br><hr>"
END SUB

SUB ErrorADOReport(parm_msg,parm_conn)
    HowManyErrs=parm_conn.errors.count
    IF  HowManyErrs=0 then
        exit sub
    END IF
    pad="&nbsp;&nbsp;&nbsp;&nbsp;"
    response.write "<b>ADO Reports these Database Error(s) executing:<br>"
        response.write SQLstmt & "</b><br>"
    for counter= 0 to HowManyErrs-1
        errornum=parm_conn.errors(counter).number
        errordesc=parm_conn.errors(counter).description
        response.write pad & "Error#=<b>" & errornum & "</b><br>"
        response.write pad & "Error description=<b>"
        response.write errordesc & "</b><p>"
    next
END SUB
%>

There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.