Tải bản đầy đủ

Tài liệu Edit Data and Update Changes That Are Made to an ADO.NET pdf

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?
Technique
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.
CommandBuilde
r
GetUpdateComman
d
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.UpdateCommand.Connect.Close()
.
DataAdapter Update Performs the update command against the
dataset.
DataSet Tables Represents a collection of tables found within
a dataset.
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
string value.
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.
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
Figure 4.1.
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
Label Name
Caption
Label1
Customer
TextBox Name
Text
txtCustLimit
A
Button Name
Caption


btnLoadList
Load List
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
Button Name
Caption
btnEdit
&Edit
Button Name
Caption
btnSave
&Save
Button Name
Caption
btnCancel
&Cancel
3. Note

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
11.
12. '-- Move the loading of the list to a subroutine for
13. ' additional(calls)
14. LoadList()
15.
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()
19.
20. Dim odaCustList As OleDb.OleDbDataAdapter
21. Dim dtCustList As DataTable = New DataTable()
22.
23. Dim strSQL As String
24.
25. '-- Create the SQL String
26. strSQL = "Select CustomerID, CompanyName " & _
27. From Customers Where CustomerID Like '" & _
28. Me.txtCustLimit.Text & "%'"
29.
30.
31. '-- Set up the exception catch
32. Try
33.
34. '-- Create an instance of the data adapter; then fill the data
35. table
36. odaCustList = New OleDb.OleDbDataAdapter(strSQL, _
37. BuildCnnStr("(local)", "Northwind"))
38. odaCustList.Fill(dtCustList)
39.
40. '-- Bind the data to the list box
41. lstCustomers.DataSource = dtCustList
42. lstCustomers.DisplayMember = "CompanyName"
43. lstCustomers.ValueMember = "CustomerID"
44.
45. LoadIndividual()
46. Catch oexpData As OleDb.OleDbException
47. MsgBox(oexpData.Message)
48. End Try
49.
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()
53.
54. Dim strSQL As String
55. Dim strName As String
56. Dim oCtl As Object
57.
58. mdsCustIndiv.Clear()
59.
60. If Me.lstCustomers.SelectedIndex <> -1 Then
61.
62. Try
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"))
68.
69. '-- Fill the dataset
70. modaCustIndiv.Fill(mdsCustIndiv, "Customers")

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×