Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  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)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO : Updating records in the database with ASP
 

Updating records in the database with ASP

by Faisal Khan.Follow Faisal Khan on Twitter

We have already discussed how to create DSN and show database contents in the ASP page in our tutorial Accessing Database from ASP. Then we moved on to learn how to add records to the database in the tutorial Adding Records to the Database. We will now see how to update the records in the database.

Like we saw in the Adding Records to the Database tutorial, we have two ways to update records; one with Connection Object using SQL update command and the other with the Recordset Object.

SQL Update Statement
Allows us to update the records already present in the database.

Syntax :

	update table_name set field1 = 'new value'
		[where field2 = 'field2_value']

update statement like insert statement takes 3 arguments. table_name is the name of the table you want to update, field1 is the name of the field ( column ) whose value you want updated and 'new value' is the new value that field1 will contain. Note where clause is optional, it is used to pick one specific record to update from the whole column. Following example will explain what I mean.

Example

	update books set title = 'Update Records' where author = 'Faisal Khan'

The above update statement will change the value of title field in the books table to 'Update Records' where author field contains 'Faiasl Khan'. Thus all titles will be converted to 'Update Records' where the author name in author field is 'Faisal Khan'.

		' Before running the update statement
		Table Name = books
		Fields			Values
		author			Faisal Khan
		title			Add Records
		' After running the update statement
		Table Name = books
		Fields			Values
		author			Faisal Khan
		title			Update Records

Now since we have seen how to deal with update statement and how to update records in the database, it is time to move on to actually look at the Connection and Recordset Object on how to update the records with ASP.

We have two options to update database records with, one is with Connection Object using update SQL statement and the other one with Recordset Object. As you will see, updating records is as easy as adding or selecting records from the database and if you have read the Adding Records to the Database tutorial then you will find that the code is almost the same with just a slight to change to update the records instead of adding it.

Via Connection Object
Here is how we update records with Connection Object :

<%
	' Setting variables
	Dim con, sql_update, data_source
	data_source = myDSN
	sql_update = "update books set title = 'Update Records' _
		where author = 'Faisal Khan'"

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

	' Executing the sql update code
	con.Execute sql_update

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

Notice it is the SQL statement executed by Connection Object which determines the selection, addition, updating or deletion of the data in the database. Here we use a update statement to update the title field to 'Update Records' where author is 'Faisal Khan'. It is same example we used in the last page.

Via Recordset Object
Recordset Object offers another easy way to update records. Here is how we update records with Recordset Object :

<%
	' Setting variables
	Dim rs, data_source
	data_source = myDSN

	' Creating Recordset Object and opening the database
	Set rs = Server.CreateObject("ADODB.Recordset")
	' Lets open books table
	rs.Open "books", data_source, 1, 2, adCmdTableDirect

	' No rs.AddNew command needed here since we are only
	' updating the records

	' Filtering records so that we get only those records
	' where author = 'Faisal Khan'
	rs.Filter = "author = 'Faisal Khan'"

	' Now updating records
	rs("title") = "Update Records"
	rs.Update

	' Done. Now Close the Connection
	rs.Close
	Set rs = Nothing
%>

As you saw we didn't use any SQL statements here. So what to use when updating records, Connection or Recordset Object ? well I personally use Recordset Object, which ever you choose is up to you.

We have already discussed how to create DSN and show database contents in the ASP page in our tutorial Accessing Database from ASP. Then we moved on to learn how to add records to the database in the tutorial Adding Records to the Database. We will now see how to update the records in the database.

Like we saw in the Adding Records to the Database tutorial, we have two ways to update records; one with Connection Object using SQL update command and the other with the Recordset Object.

SQL Update Statement
Allows us to update the records already present in the database.

Syntax :

	update table_name set field1 = 'new value'
		[where field2 = 'field2_value']

update statement like insert statement takes 3 arguments. table_name is the name of the table you want to update, field1 is the name of the field ( column ) whose value you want updated and 'new value' is the new value that field1 will contain. Note where clause is optional, it is used to pick one specific record to update from the whole column. Following example will explain what I mean.

Example

	update books set title = 'Update Records' where author = 'Faisal Khan'

The above update statement will change the value of title field in the books table to 'Update Records' where author field contains 'Faiasl Khan'. Thus all titles will be converted to 'Update Records' where the author name in author field is 'Faisal Khan'.

		' Before running the update statement
		Table Name = books
		Fields			Values
		author			Faisal Khan
		title			Add Records
		' After running the update statement
		Table Name = books
		Fields			Values
		author			Faisal Khan
		title			Update Records

Now since we have seen how to deal with update statement and how to update records in the database, it is time to move on to actually look at the Connection and Recordset Object on how to update the records with ASP.

We have two options to update database records with, one is with Connection Object using update SQL statement and the other one with Recordset Object. As you will see, updating records is as easy as adding or selecting records from the database and if you have read the Adding Records to the Database tutorial then you will find that the code is almost the same with just a slight to change to update the records instead of adding it.

Via Connection Object
Here is how we update records with Connection Object :

<%
	' Setting variables
	Dim con, sql_update, data_source
	data_source = myDSN
	sql_update = "update books set title = 'Update Records' _
		where author = 'Faisal Khan'"

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

	' Executing the sql update code
	con.Execute sql_update

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

Notice it is the SQL statement executed by Connection Object which determines the selection, addition, updating or deletion of the data in the database. Here we use a update statement to update the title field to 'Update Records' where author is 'Faisal Khan'. It is same example we used in the last page.

Via Recordset Object
Recordset Object offers another easy way to update records. Here is how we update records with Recordset Object :

<%
	' Setting variables
	Dim rs, data_source
	data_source = myDSN

	' Creating Recordset Object and opening the database
	Set rs = Server.CreateObject("ADODB.Recordset")
	' Lets open books table
	rs.Open "books", data_source, 1, 2, adCmdTableDirect

	' No rs.AddNew command needed here since we are only
	' updating the records

	' Filtering records so that we get only those records
	' where author = 'Faisal Khan'
	rs.Filter = "author = 'Faisal Khan'"

	' Now updating records
	rs("title") = "Update Records"
	rs.Update

	' Done. Now Close the Connection
	rs.Close
	Set rs = Nothing
%>

As you saw we didn't use any SQL statements here. So what to use when updating records, Connection or Recordset Object ? well I personally use Recordset Object, which ever you choose is up to you.


 ( No Further Pages )

See all comments and questions (post-ad) posted for this tutorial.


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. Inserting Form content into Database with ASP
  10. Using ASP pages to page through Recordsets

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

  1. Updata the records using MS Access, ASP
  2. Select + update not working together
  3. Switch to Connection Object Update after using Recordset Object
  4. I get the following error when trying to update my record
  5. Why don't null fields get updated when using
  6. Updating an access database

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 to.

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