Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Stay Informed
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (29)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
Re: Unable to insert data in an Access databa..
by Faisal Khan on 12 Nov 2008 Go To Post

Re: HOW TO MOVE FROM APACHE TO MS IIS
by Faisal Khan on 12 Nov 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by smithsan on 19 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by peeru999 on 8 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by Faisal Khan on 8 Oct 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO : Inserting Form content into Database with ASP
 

Inserting Form content into Database with ASP
by Faisal Khan.

In this article we will create a simple HTML Form and an ASP page. Any data which is entered by the user in that HTML Form will be received by the ASP action page and will enter that data into the Access database. After that we will create an ASP page to show all the records entered and to delete the specific records if wanted.

HTML Form Page
Create a new HTML page and name it 'form.htm'. Copy the following code and paste it into the body section of 'form.htm' page :

Inserting Records :
<form action="form_ac.asp" method="post">
	Name : <input type="text" name="name"><br>
	Email : <input type="text" name="email"><br>
	Country : <input type="text" name="country"><br>
	Comments : <textarea name="comments" cols="20" rows="5"></textarea><br>
	<input type="submit" value="submit">
</form>

The above HTML code creates a simple Form asking for user name, email, country and comments. You can later change them according to what you want your users to enter. Although the above code does what it is supposed to do, to see a better looking page, you might want to download the zip file at the end of this article.

ASP Action Page
Create a new ASP page 'form_ac.asp' in the same directory as the above HTML page. Copy and paste the following ASP code into 'form_ac.asp' page :

<%
' Declaring variables
Dim name, email, country, comments, data_source, con, sql_insert

' A Function to check if some field entered by user is empty
Function ChkString(string)
	If string = "" Then string = " "
	ChkString = Replace(string, "'", "''")
End Function

' Receiving values from Form
name = ChkString(Request.Form("name"))
email = ChkString(Request.Form("email"))
country = ChkString(Request.Form("country"))
comments = ChkString(Request.Form("comments"))
data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
Server.MapPath("form.mdb")
sql_insert = "insert into users (name, email, country, comments) values ('" & _
	name & "', '" & email & "', '" & country & "', '" & comments & "')"

' Creating Connection Object and opening the database
Set con = Server.CreateObject("ADODB.Connection")
con.Open data_source
con.Execute sql_insert

' Done. Close the connection
con.Close
Set con = Nothing
%>

Our above ASP page 'form_ac.asp' will receive values entered by user in the HTML Form and will first check to see if some fields have been left empty. Then it will insert those values into our Access database. Simple.

Simple Access Database
Now we will create a simple Access database with just one table 'users' containing five fields, 'id', 'name', 'email', 'country' and 'comments' respectively. If you are not comfortable with creating simplest of Access databases then you might want to read the database tutorial I have compiled for you. Note that at the end of this article you can download the sample database and code mentioned in this article. Open Microsoft Access and create a database 'form.mdb' and save it in the same directory where you are creating other HTML and ASP files for this tutorial. In 'design view' of Microsoft Access our 'users' table will look like :

Users - Table
Users - Table

We will now move on to create ASP pages where we can see all the records entered into the database and from where we can delete any records we want.

Lets first create an ASP page to show the records entered by users. You will also be able to delete records by pressing the 'delete' button on the corresponding record.

Showing All Records
Create a new 'showall.asp' ASP page and copy paste the following code into it :

<%
' Declaring variables
Dim rs, data_source, no
no = 0
data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
	Server.MapPath("form.mdb")

' Creating Recordset Object and opening the database
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "users", data_source

' Looping through the records to show all of them
While Not rs.EOF
	Response.Write "<form action='del.asp' method='post'>"
	Response.Write "Name : " & rs("name") & "<br>"
	Response.Write "Email : " & rs("email") & "<br>"
	Response.Write "Country : " & rs("country") & "<br>"
	Response.Write "Comments : " & rs("comments") & "<br>"
	Response.Write "<input type='hidden' name='id' value='" & rs("id") & "'>"
	Response.Write "<input type='submit' value='Delete'>" & "<br>"
	Response.Write "</form>"
	no = no + 1
	rs.MoveNext
Wend

' Done. Now close the Recordset
rs.Close
Set rs = Nothing

Response.Write "<p>Total Records Found : " & no
%>

Above ASP page 'showall.asp' shows all the records in the database. It also allows you to delete the specific record if you want to. We are now almost done except that we also need to create the 'del.asp' page which we mentioned in the above code so that you can delete the records if you want.

Records Deletion Page
Create a new 'del.asp' ASP page and save it in the same directory where you have been saving other HTML and ASP pages discussed in this article. Copy and paste the following code into it :

<%
If Len(Request.Form("id")) Then
	' Declaring variables
	Dim form_id, data_source, sql_delete, con
	form_id = CInt(Request.Form("id"))
	data_source =  "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
			Server.MapPath("form.mdb")
	sql_delete = " delete from users where id = " & form_id

	' Creating Connection Object and opening the database
	Set con = Server.CreateObject("ADODB.Connection")
	con.Open data_source
	con.Execute sql_delete
	con.Close
	Set con = Nothing

	Response.Write "Record No. : " & form_id & _
		" was successfully deleted from the database."
Else
	Response.Redirect "showall.asp"
End If
%>

The above ASP code deletes the records based on the 'id' field received from the 'showall.asp' page.


 ( No Further Pages )

Download Associated Files
2000040101.zip

Related Articles
  1. How to display records from top 4 database systems using plain ASP?
  2. Uploading Files into an Access Database using plain ASP
  3. Displaying Images from an Access Database using plain ASP
  4. Fastest way of Database Access : Caching Records in Memory
  5. Adding records to the database with ASP
  6. DSN vs DSN less Database Connections
  7. Searching and sorting records in a recordset from the database
  8. Speedup Database Access using GetRows
  9. Using ASP pages to page through Recordsets
  10. Generating Random Records from the Database

Comments/Questions ( Threads: 24, Comments: 45 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Error upon submitting form on website using above code ( 1 Reply )
  2. There is a problem related with database
  3. Get an email from each record
  4. ChkString(Request.Form(
  5. Records added to the database are blank ( 2 Replies )
  6. POST is not allowed for the URL /form_ac.asp ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  7. About 'form_ac.asp' for ASP Action page ( 1 Reply )
  8. Please help me with your article ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  9. Method POST not allowed ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  10. Nevermind
  11. great, one small question
  12. Problem with Inserting Data in ASP ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  13. Syntax error in INSERT INTO statement. ( 1 Reply )
  14. have problem inserting content into database ( 3 Replies )
  15. an important Question
  16. Faisal, problem with Inserting Form content into Database with ASP!! ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  17. Form won't insert into Access
  18. Record Specific URL access to data entry page!!!!!!
  19. ASP Action Page
  20. ASP Online Form to Access Database!!! Need Help!
  21. Faisal, your script didn't write to the database ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  22. won't work on 2000 svr- what's going on
  23. Inserting Form content into Database with ASP
  24. HELP ME! NEED HELP FOR DREAMWEAVER ULTRA DEV WITH MS ACCESS USING ASP ( 6 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserved.