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
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.
Now that the button is in the page you need to set its properties.
To set the button's properties, select Properties from the menu.
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.
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.
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.
Click Script Outline.
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.
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.
There it is, the onclick event. To display the code block for this event, double-click the event.
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.
text area: line between Sub InsertBtn_onclick() and End Sub, enter CustRecords.addRecord
To view the page in the browser, select View in Browser from the menu.
Here is the page with one record displayed. To begin entering a new record, just click the Insert button.
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.
ADDR2: Riverside Dr
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.