Web Design Tutorials ASP web site design tutorial
Inserting Rows into
Inserting Rows into a Database
This tutorial will show you how to insert a row of data into a database using ASP and an MS Access database. You can do it on one ASP file, but we'll have two files: one processor page (proc.asp), and one form page (form.asp). Then, we'll make the MS Access database that corresponds to the ASP.
Part I - Creating the processing page: proc.asp
This page will catch the form data, open a connection with the Access database (change "test.mdb" to your database), insert the row using a sql query, and close the connection. If it executes fine, it will print out "done inserting row".
Here's the code:
<%
Dim Conn
Dim SqlTemp
If request("insrow") = "1" THEN
psubject = request("pSubj")
pdate = request("pDate")
ptext = request("pText")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.connectionstring =
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Server.MapPath("test.mdb")& ";"
Conn.Open
SQLTemp = "INSERT INTO tblTest (pSubj, pDate, pText)
VALUES ('" & psubject & "', " & pdate & ",
'" & ptext & "')"
Conn.execute(SQLTemp)
Conn.Close
Set Conn = Nothing
response.write("<p>done inserting row</p>")
End If
%>
|
Part II - Creating the form: form.asp
Here's a simple form that will use the "post" method to submit the info to the proc.asp page:
<form name="frmInsert" method="post" action="test.asp">
<input type="hidden" name="insrow" value="1">
Subject: <input type="text" name="pSubj" size="13"><br>
Date: <input type="text" name="pDate" size="13"><br>
Text: <input type="text" name="pText" size="13"><br>
<input type="submit" size=8 value="Submit">
</form>
|
Part III - creating the database: test.mdb
Make a table in the database called "tblTest" (you may change it, but make sure it corresponds with the above table name). Inside the database, make an Autonumber row called "pSubj" which is the Primary Key, a date row called "pDate", and a row called "pText" with the memo row-type.
And there you have it, a database, a form, and a processor ready for data entry!
Note: Had trouble connecting to the database? Click here.
| Rate This Material: |
Bad |
Excellent |
|
| |

© 2010 www.kagemedia.com
Kelowna web design and development | Kelowna web hosting
Kelowna website marketing | Kelowna database design |
Links
Serving:
Kelowna BC - Penticton BC - Vernon BC - Westbank BC - Winfield BC
|
|
 |
Win a FREE web design!
We are giving away a completely free web design and web hosting package every month.
Click here for details |
|
|
|