- Start Microsoft Visual Studio or Visual C# if necessary and create
a new Windows 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 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 |
txtGenderID |
dsPersons1 - Persons.GenderID |
- 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 void Form1_Load(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsPersons1, "Persons");
}
|
- Double-click the | <, the <<, the >>, and the < |
buttons then implement their Click events as follows:
private void btnFirst_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsPersons1, "Persons"].Position = 0;
}
private void txtPrevious_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsPersons1, "Persons"].Position =
this.BindingContext[dsPersons1,
"Persons"].Position - 1;
}
private void btnNext_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsPersons1, "Persons"].Position = this.BindingContext[dsPersons1, "Persons"].Position + 1;
}
private void btnLast_Click(object sender, System.EventArgs e)
{
this.BindingContext[dsPersons1, "Persons"].Position = this.BindingContext[dsPersons1, "Persons"].Count - 1;
}
|
- Press Ctrl + F5 to test the application
|