FunctionX Practical Learning Logo

Regular Data Binding


Introduction

This is an example of how to associate regular controls like text boxes with data from a table.

Practical LearningPractical Learning: Creating the Database

  1. Start the Enterprise Manager (Start -> (All) Programs -> Microsoft SQL Server -> Enterprise Manager)
  2. Create a new database named People
  3. Create a new table in Design view as follows:
     
    Column Name Data Type Length Addition Properties
    PersonID int   Set Primary Key
    Identity: Yes
    FirstName varchar 20  
    LastName varchar 20 Allow Nulls: Unchecked
  4. Save it as Persons
  5. Click Close
    You can fill out the table with a few records
 

Data Binding Text Boxes

.

Practical LearningPractical Learning: Binding Text Boxes

  1. Start Microsoft Visual Studio or Visual C++ if necessary and create a new Windows Forms Application named ExoADO1
  2. Design the form as follows:
     
    Regular Data Binding
    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 > |  
  3. Position the mouse on the Toolbox to display its list of objects and click Data
  4. From the Data section, click SqlDataAdapter and click the form
  5. Click Next
  6. 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
  7. Click Next
  8. Accept the default Use SQL Statements radio button and click Next
  9. Click Query Builder...
  10. From the Tables property page of the Add Table dialog box, click Persons
  11. Click Add and click Close
  12. In the upper section of the Query Builder window, click the PersonID, FirstName, LastName, and GenderID check boxes
     
  13. Click OK
  14. Click Next to see a list of the procedures or statements that will be created and click Finish
  15. On the main menu, click Data -> Generate Dataset...
  16. Click Next to the New radio button if necessary and change the name of the dataset from DataSet1 to dsPersons
  17. Click OK
  18. On the form, click the text box on the right side of PersonID
  19. 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
  20. In the list, click PersonID
  21. Notice that the field now displays dsPersons1 - Persons.PersonID
  22. Configure the other TextBox controls as follows:
     
    Control DataBings -> Text
    txtPersonID dsPersons1 - Persons.PersonID
    txtFirstName dsPersons1 - Persons.FirstName
    txtLastName dsPersons1 - Persons.LastName
  23. 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
  24. 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
  25. Press Ctrl + F5 to test the application
 

Home Copyright © 2004-2010 FunctionX, Inc.