
Inserting Data by Using the Business Component
As with other data source controls, such as the SqlDataSource control, the ObjectDataSource control supports updating (inserting, updating, and deleting). In this section, you will modify the business component with a method that inserts an author record. Then you will change the page so that users can type new author information and modify the ObjectDataSource control to perform the insertion.
Note |
|---|
| During this part of the walkthrough, the Authors.xml file you created previously will be updated. It is important that the application have permission to write to the file at run time or the Web page will display an error when you try to update the file. If you created the Authors.xml file in the App_Data folder, permissions are set automatically. |
To modify the business component to allow inserts
-
Switch to the BusinessObject file.
-
Add the following method as the final member of AuthorClass.
Public Sub InsertAuthor(ByVal au_id As String, _
ByVal au_lname As String, _
ByVal au_fname As String, ByVal au_phone As String)
Dim workRow As DataRow = dsAuthors.Tables(0).NewRow
workRow.BeginEdit()
workRow(0) = au_id
workRow(1) = au_lname
workRow(2) = au_fname
workRow(3) = au_phone
workRow.EndEdit()
dsAuthors.Tables(0).Rows.Add(workRow)
dsAuthors.WriteXml(filePath, Data.XmlWriteMode.WriteSchema)
End Sub
public void InsertAuthor (String au_id, String au_lname,
String au_fname, String au_phone)
{
DataRow workRow = dsAuthors.Tables[0].NewRow ();
workRow.BeginEdit ();
workRow[0] = au_id;
workRow[1] = au_lname;
workRow[2] = au_fname;
workRow[3] = au_phone;
workRow.EndEdit ();
dsAuthors.Tables[0].Rows.Add (workRow);
dsAuthors.WriteXml (filePath, XmlWriteMode.WriteSchema);
}
Note |
|---|
| Pay close attention to the names of the variables used to pass author information into the method (au_id, au_lname, au_fname, and au_phone). They must match the column names defined in the schema of the XML file you created previously. |
The new method takes four values to insert, which you will provide in the page as parameters. The method creates a new row in the dataset, and then writes the updated dataset out as an XML file.
-
Save the file.
The next step is to change the page so that users can enter new author information. For the following procedure, you will use the DetailsView control.
To add a control for inserting data
-
Switch to or open the Default.aspx page.
-
Switch to Design view.
-
In the Toolbox, from the Data folder, drag a DetailsView control onto the page.
Note |
|---|
| The exact layout of the page is not important. |
-
On the DetailsView Tasks menu, in the Choose Data Source box, click AuthorsObjectDataSource.
Note |
|---|
| If the DetailsView Tasks menu is not visible, click the smart tag. |
-
In the Properties window, set AutoGenerateInsertButton to true.
This causes the DetailsView control to render a New button that users can click to put the control into data-entry mode.
Finally, you must configure the ObjectDataSource control to specify what action the control should take to insert data.
To configure the data source control for inserting data
-
Right-click AuthorsObjectDataSource, click Properties, and then set InsertMethod to InsertAuthor.
This is the name of the method that you added to the business component.
You can now insert new authors into the XML file.
To test insertion
-
Press CTRL+F5 to run the Default.aspx page.
-
In the DetailsView control, click the New button.
The control is redisplayed with text boxes.
-
Enter new author information, and then click Insert.
The new author information is added to the XML file. The GridView control immediately reflects the new record.