Modifying Data  «Prev  Next»

Lesson 3Inserting records
Objective Insert new records into the database using bound controls.

Inserting Database Records into SQL-Server Database

The VirtualBookShelf.com Web site wouldn't last too long if you couldn't add customers and books to the database.
Visual InterDev makes it just as easy to insert a table row as it does to update a row.
You will see how this is done in the simulation that follows. Then you will get the opportunity to do this yourself in an exercise.

Inserting New Records using Bound Controls

  1. We will Begin with the file CustManage_1.asp opened in the source editor. First, add the Insert button to the page. For the purpose of this simulation, you only need to click the Button in the list on the left side of the screen and we'll drag the button into the page for you.
  2. Now that the button is in the page you need to set its properties.
  3. To set the button's properties, select Properties from the menu.
  4. There are only two properties that need to be set: a name to uniquely identify this button, and a caption to display on the button. Name the button InsertBtn and set the caption to Insert. Then click the OK button.
  5. Now functionality can be added to the Insert button. Begin by opening the Script Outline window. This is done from the View drop-down menu.
  6. The View menu is displayed. Place the mouse pointer over Other Windows. However, for the purpose of this simulation, you will need to click Other Windows.
  7. Click Script Outline.
  8. The Insert button is an object. Objects are listed under Server Objects & Events. Click the + to the left of Server Objects & Events to expand the list.
  9. The list is expanded and the Insert button is the third item down. You need to add code to the Insert button's onclick event. To access the event, click the + to the left of InsertBtn to expand its list of events.
  10. There it is, the onclick event. To display the code block for this event, double-click the event.
  11. All that you have to do now is add one simple statement to add insert capability to the page. The statement will call the recordset object's addRecord method. After you enter the statement, view the page in the browser to see it in action.
  12. text area: line between Sub InsertBtn_onclick() and End Sub, enter CustRecords.addRecord
  13. To view the page in the browser, select View in Browser from the menu.
  14. Here is the page with one record displayed. To begin entering a new record, just click the Insert button.
  15. A blank record is provided so new customer information can be added. A word of caution: CustManage.asp doesn't have error handling code. The CustNo is a unique field and we will get an error if we enter a number that is already in use. So we want to make sure we enter a unique 10-digit customer number for each new customer added. Enter the following customer information for the field indicated, and click Update when you are finished.

CustNo: 0000000030
LName: Bond
FName: Ian
ADDR1: 1323
ADDR2: Riverside Dr
City: Cumberland
ST: MD
Zip: 86007 
Status: A
  • Clicking the Update button writes the new information to the customer table. The Update function was added in the previous lesson to update existing information, and it can also be used to insert new information. This is because the addRecord method adds a blank record, then the updateRecord method updates it with the real information. This is the end of the simulation.

  • In the next lesson, you will see that deleting rows is easier than inserting them.

    Inserting Records - Exercise

    Click the Exercise link below to practice inserting new records into the BookTable.
    Inserting Records - Exercise