4.1 Edit Data and Update Changes That Are Made to an ADO.NET DataSet Object
Listing and viewing data is easy. What you really need to do is to be able to edit and
update data. You know you can use the DataSet object and some of its objects and
methods to perform this task. How do you edit and update data using the DataSet object?
In this How-To, you will use the DataAdapter, DataSet, DataTable, and DataRow
objects. You have experienced some of the properties and methods of each of these
objects before. In this chapter, you are going to be using the following properties and
methods that are shown in Table 4.1.
Table 4.1. DataAdapter, DataSet, DataTable, and DataRow Properties and Methods
Object /Method Property Description
DataAdapter Fill Fills DataSet and DataTable objects.
Creates an Update command and places it into
the data adapter's UpdateCommand property.
DataAdapter UpdateCommand Holds the SQL statement for the update.
DataAdapter Close Closes the connection off the
UpdateCommand. The syntax is
DataAdapter Update Performs the update command against the
DataSet Tables Represents a collection of tables found within
DataSet Rows Contains a collection of rows within a
specified table in a dataset.
DataSet AcceptChanges Sends the changes back to the server.
DataRow ToString Retrieves the data from the column that is
specified in the DataRow and returns it as a
DataRow BeginEdit Begins the editing of a DataRow, allowing
you to replace values in the columns.
DataRow EndEdit Completes the editing of a DataRow.
You will see these objects with their properties and methods used in the following steps.
Open and run the VB.NET-Chapter 4 solution. From the main form, click on the
command button with the caption How-To 4.1. When the form loads, click on the Load
List button to display the customers that begin with the letter A. Click the Edit button.
You will notice that the fields have now taken on a sunken look. Place the cursor into the
City field and change the value to Dunkirk. Now click Save. If you move off the record
and move back on, you will notice that the value has been saved.
This form looks similar to the form created in Chapter 1. The difference is that this time
you will not be using controls that are bound at design time. You can see the form in
1. Create a new Windows Form.
2. Add the following controls, setting the properties as listed in Table 4.2.
Table 4.2. Controls Property Settings
Object Property Setting
ListBox Name lstCustomers
Label Caption Customer ID
Label Caption Company Name
Label Caption Contact
Label Caption Contact Title
Label Caption Address
Label Caption City
Label Caption Region
Label Caption Country
Label Caption Phone
Label Caption Fax
TextBox Name txtCustomerID
TextBox Name txtCompanyName
TextBox Name txtContact
TextBox Name txtContactTitle
TextBox Name txtAddress
TextBox Name txtCity
TextBox Name txtRegion
TextBox Name txtPostalCode
TextBox Name txtCountry
TextBox Name txtPhone
TextBox Name txtFax
Notice that the Text property of the text boxes is not being set at
design time. In Chapter 1, "Developing Windows Forms Using
Bound Controls," they were set to columns of a dataset that was
included on the form. In this How-To, they will be set at run-time.
4. In the class module for the form, add the following three Private declarations just
below the line of code that reads Windows Form Designer generated code. These
three objects will be used throughout the form.
5. Dim mdsCustIndiv As New DataSet()
6. Dim modaCustIndiv As OleDb.OleDbDataAdapter
7. Dim mdrCustIndiv As DataRow
8. Enter the following code as the Click event for btnLoadList:
9. Private Sub btnLoadList_Click(ByVal sender As System.Object, _
10. ByVal e As System.EventArgs) Handles btnLoadList.Click
12. '-- Move the loading of the list to a subroutine for
13. ' additional(calls)
16. End Sub
17. Create the LoadList routine by entering the following code into the form you
created for this How-To. This code creates and fills a data table using a data
adapter. The string that the data adapter uses creates a Select statement by using
the txtCustLimit text box. The DataSource, DisplayMember, and ValueMember
properties of the list box are then bound. Last, the LoadIndividual routine is called,
which is described in the next step.
18. Private Sub LoadList()
20. Dim odaCustList As OleDb.OleDbDataAdapter
21. Dim dtCustList As DataTable = New DataTable()
23. Dim strSQL As String
25. '-- Create the SQL String
26. strSQL = "Select CustomerID, CompanyName " & _
27. From Customers Where CustomerID Like '" & _
28. Me.txtCustLimit.Text & "%'"
31. '-- Set up the exception catch
34. '-- Create an instance of the data adapter; then fill the data
36. odaCustList = New OleDb.OleDbDataAdapter(strSQL, _
37. BuildCnnStr("(local)", "Northwind"))
40. '-- Bind the data to the list box
41. lstCustomers.DataSource = dtCustList
42. lstCustomers.DisplayMember = "CompanyName"
43. lstCustomers.ValueMember = "CustomerID"
46. Catch oexpData As OleDb.OleDbException
48. End Try
50. End Sub
51. Create the LoadIndividual routine by entering the following code in the form you
created for this How-To. Taking the SelectedItem from the list box, a data adapter
is created, and a dataset is filled. Next, the individual DataRow is created. Last,
each of the TextBox controls is loaded with the value from the column with the
corresponding name. Notice the use of the Try-Catch-End-Try to ignore controls
that don't have a like column in the DataRow.
52. Private Sub LoadIndividual()
54. Dim strSQL As String
55. Dim strName As String
56. Dim oCtl As Object
60. If Me.lstCustomers.SelectedIndex <> -1 Then
63. '-- Load the individual record into the dataset
64. strSQL = "Select * from Customers Where CustomerID = '" &
65. Me.lstCustomers.SelectedItem(0) & "'"
66. modaCustIndiv = New OleDb.OleDbDataAdapter(strSQL, _
67. BuildCnnStr("(local)", "Northwind"))
69. '-- Fill the dataset
70. modaCustIndiv.Fill(mdsCustIndiv, "Customers")