- Start Microsoft Visual Studio or Visual C++ if necessary and create
a new Windows Forms Application named ExoADO1
- Design the form as follows:
|
Control |
Name |
Text |
Additional Properties |
Label |
|
Person ID: |
|
TextBox |
txtPersonID |
|
AlignText: Right |
Label |
|
First Name: |
|
TextBox |
txtFirstName |
|
|
Label |
|
Last Name: |
|
TextBox |
txtLastName |
|
|
Label |
|
Gender: |
|
TextBox |
txtGender |
|
AlignText: Right |
Button |
btnFirstRecord |
| < |
|
Button |
btnPreviousRecord |
<< |
|
Button |
btnNextRecord |
>> |
|
Button |
btnLastRecord |
> | |
|
|
- Position the mouse on the Toolbox to display its list of objects and
click Data
- From the Data section, click SqlDataAdapter and click the form
- Click Next
- Click the arrow of the combo box. If you see Server.People.dbo,
select it. If you don't see it, click New Connection. In the top combo box, select the server that contains the above
database. (Either) Click the Use Windows NT Integrated Security (or
click the Use A Specific User Name And Password, in which case you
would need to type the username and password). In the Select The Database On The Server combo box, select for the above
People database. Click OK. Back in the Data Adapter Configuration Wizard, in the combo box,
select People if necessary
- Click Next
- Accept the default Use SQL Statements radio button and click Next
- Click Query Builder...
- From the Tables property page of the Add Table dialog box, click
Persons
- Click Add and click Close
- In the upper section of the Query Builder window, click the PersonID,
FirstName, LastName, and GenderID check boxes
- Click OK
- Click Next to see a list of the procedures or statements that will be
created and click Finish
- On the main menu, click Data -> Generate Dataset...
- Click Next to the New radio button if necessary and change the name of the dataset from
DataSet1 to dsPersons
- Click OK
- On the form, click the text box on the right side of PersonID
- In the Properties window,
click the + button of DataBindings. Then click Text to reveal its
combo box. Click arrow of the combo box. In the list, click the +
button of dsPersons1 and click the + button of Persons
- In the list, click PersonID
- Notice that the field now displays dsPersons1 - Persons.PersonID
- Configure the other TextBox controls
as follows:
Control |
DataBings -> Text |
txtPersonID |
dsPersons1 - Persons.PersonID |
txtFirstName |
dsPersons1 - Persons.FirstName |
txtLastName |
dsPersons1 - Persons.LastName |
- To display the data when the form comes up, double-click an empty
area on the form and implement its Load event as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SqlDataAdapter1.Fill(Me.DsPersons1, "Persons")
End Sub
|
- Double-click the | <, the <<, the >>, and the < |
buttons then implement their Click events as follows:
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
Me.BindingContext(Me.DsPersons1, "Persons").Position = 0
End Sub
Private Sub txtPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPrevious.Click
Me.BindingContext(Me.DsPersons1, "Persons").Position = _
Me.BindingContext(Me.DsPersons1, "Persons").Position - 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Me.BindingContext(Me.DsPersons1, "Persons").Position = _
Me.BindingContext(Me.DsPersons1, "Persons").Position + 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
Me.BindingContext(Me.DsPersons1, "Persons").Position = _
Me.BindingContext(Me.DsPersons1, "Persons").Count - 1
End Sub
|
- Press Ctrl + F5 to test the application
|