Data Binding In .NET

Introduction

 
Data Binding is a very powerful feature of . NET. Data binding allows the user to access information from databases, arrays, or collections.
 

Uses of Data Binding

 
Report Generation
 
It is a useful and efficient feature of Microsoft Visual Studio. It is used to display the summarized information to the users. In reports, data binding is used to fetch records based on a search criterion. This criterion acts as a parameter to fetch the required records from the table. By using the data binding technique, the required data can be bound and displayed to the user.
 
Data Entry
 
Data binding can be used for data entry tasks, where users can link controls like textboxes or drop-down lists to records in the database. This can help to insert, updating and deleting records from the database.
 

BindingManagerBase Class

 
The BindingManagerBase class is used to coordinate with the objects of the Binding class. The objects of the Binding class help in creating and managing simple binding, wherein the control is bound to a single field of a table. The BindingManagerBase class is useful in the coordination of controls bound to the same data source. For example, consider that a form has two TextBox controls. These controls are bound to the Employee table but to two different columns such as Address and City. The two controls must display accurate values from their respective fields at any point in time. This will be ensured only when there is coordination between the bound controls. This coordination is implemented using the BindingManagerBase class.
 
The BindingManagerBase class is an abstract class and provides a derived class namely, Currency Manager. The class maintains the coordination using a pointer to retrieve the accurate values.
 
Properties, Methods, and Events of BindingManagerBase Class
  • Bindings:  This property retrieves a set of bindings being managed.
  • Count: This property retrieves the total number of rows being handled by the BindingManagerBase when used in the derived class.
  • IsBindingSuspended: This property retrieves a value that indicates whether the binding is suspended.
  • SuspendBinding: This method suspends the binding process to restrict any changes that can update the bound data source.
  • CurrentChanged:  This event occurs when the item, which is currently bound changes.

BindingContext Class

 
The BindingContext class is used to handle the BindingManagerBase objects. The object of the class helps in binding multiple data sources. As it can bind multiple data sources, it allows users to work with multiple tables. By default, a form can be containing only one BindingContext object.
 
Source code creates two TextBox controls and binds them to the database using the BindingContext class.
  1. TextBox txtOld = new TextBox();  
  2. TextBox txtNew = new TextBox();  
  3. SqlConnection sqlconABTransPort = new SqlConnection(“Data Source = MYDATA\\ SQLEXPRESS; Initial Catalog = ABTransport; Integrated Security = SSIP”);  
  4. BindingContext bndconFiles = new BindingContext();  
  5. SqlDataAdapter sqldaABTransport = new SqlDataAdapter(“SELECT * FROM Files”, sqlconABTransPort);  
  6. DataSet dsetABTransport = new DataSet();  
  7. sqldaABTransport.Fill(dsetABTransport, ”Files”);  
  8. bndconFiles = this.BindingContext;  
  9. if (!bndconFiles.Contains(dsetABTransport, “Files”)) {  
  10.     txtOld.DataBindings.Add(“Text”, dsetABTransport, “Files.OldFile”);  
  11.     txtNew.DataBindings.Add(“Text”, dsetABTransport, “Files.NewFile”);  
  12. }  
  13. bndconFiles[dsetABTransport, ”Files”].Position = 0;  
In this source code, the two TextBox controls are created and a SqlConnection is established. An instance of the BindingContext class is created namely, bndconFiles. An instance of the SqlDataAdapter class is created, which passes the SQL command and SqlConnection, object as parameters. A DataSet is created and the fill() method fills the DataSet with the records of the Files table. If the BindingContext object contains the records from the Files table, the DataBindings property binds the OldFile and NewFile columns of the Files table to appropriate TextBox controls.
 

CurrencyManager Class

 
The Control is bound to a data source and is associated with a CurrencyManager. The CurrencyManager class maintains the current position of a record within the data source.
 
Methods and Events of CurrenyManager Class
 
A control that is bound to a data source is associated with a CurrencyManager. The CurrencyManager class maintains the current position of a record within the data source.
  • CheckEmpty: This method throws an exception if there are no items in the list.
  • OnPositionChanged:  This method triggers the PositionChanged event.
  • OnItemChanged:  This method triggers the ItemChanged event.
  • ItemChanged:  This event occurs when the current item has changed.
  • PositionChanged:  This event occurs when the value of the position property has been modified.
The following source code creates TextBox and ComboBox controls that display the records from the database using the CurrencyManager class.
  1. SqlConnection sqlconRoxSteel = new SqlConnecttion(“Data Source = MYDATA\\ SQLEXPRESS; Initial Catalog = RoxSteel; Integrated Security = SSIP”);  
  2. CurrencyManager cmgRoxSteel;  
  3. SqlDataAdapter sqldaRoxSteel = new SqlDataAdapter(“SELECT * from Plants”, sqlconRoxSteels);  
  4. DataSet dsetRoxSteel = new DataSet();  
  5. sqldaRowSteel.Fill(dsetRoxSteel, ”Plants”);  
  6. Button btnNext = new Button();  
  7. btnNext.Text = ”Next”;  
  8. ComboBox cboArea = new ComboBox();  
  9. cboArea.DataSource = dsetRoxSteel.Tables(“Plants”);  
  10. cboArea.DisplayMember = ”Area”;  
  11. cboArea.DataBindings.Add(“Text”, destRoxSteel, ”Plants.Area”);  
  12. cmgrRoxSteel = (CurrencyManager) this.BindingContext(destRoxSteel, “Plants”);  
  13. cmgrRoxSteel.Postition = 0;  
  14. private void btnNext_Click(object sender, EventArgs e) {  
  15.     cmgrRoxSteel.Position += 1;  
  16. }  
In this source code, a SqlConnection is established. The SqlDataAdapter object is created that passes the SQL Command and SqlConnection object as parameters. A DataSet is created and the fill() method fills the DataSet with the records of the Plants table. A ComboBox control is created and the DataSource property of the combo box is set to the DataSet object, which contains the records of the Plants table. The DisplayMember property of the combo box is set to the Area column of the Plants table. The DataBindings property binds the Area column of the Plants table to the combo box. The CurrencyManager object binds the dataset using the binding context of the form.
 
The following source code demonstrate how to use the CurrentChanged event of the CurrencyManager class.
  1. SqlConnection sqlconDivineCare = new SqlConnection(“Data Source = MYDATA\\ SQLEXPRESS; Initial Catalog = DivineCare; Intergrated Security = SSIP”);  
  2. SqlDataAdapter sqldaDivineCare;  
  3. DataSet dsetDivineCare;  
  4. CurrencyManager cmgrDivineCare;  
  5. Label lblRecordNumber;  
  6. Button btnNext;  
  7. TextBox txtSpecialization;  
  8. sqldaDivineCare = new SqlDataAdapter(“Select * from Doctors”, sqlconDivineCare);  
  9. dsetDivineCare = new DataSet();  
  10. sqldaDivineCare.Fill(dsetDivineCare, “Doctors”);  
  11. btnNext = new Button();  
  12. lblRecordNumber = new Label();  
  13. txtSpecialization = new TextBox();  
  14. txtSpecialization.DataBindings.Add(“Text”, dsetDivineCare, ”Doctors.Specialization”);  
  15. cmgrDivineCare = (Currencymanager) this.BindingContext[dsetDivineCare, “Doctors”];  
  16. cmgrDivineCare.Position = 0;  
  17. private void btnNext_Click(object sender, EventArgs e) {  
  18.     cmgrDivineCars.Position += 1;  
  19. }  
  20. private void cmgrDivineCare_CurrentChanged(object sender, EventArgs e) {  
  21.     lblRecordNumber.Text = ”Record: ”+(cmgrDivineCare.Position + 1);  
  22. }  
In source code, a SqlConnection is established and an instance of the SQLDataAdapter is created.
 
A DataSet is created and the fill() method fills the DataSet with the records of the Doctors table The TextBox control is created which is bound to the Specialization field of the Doctors table using the DataBindings property. The CurrencyManager object is bound to the DataSet using the BindingContext object of the form. The position of the CurrencyManager object is set too using the Position property of the CurrencyManager class. When the button is clicked, the position of the CurrencyManager objects changes and is set to the second record. This triggers the CurrentChanged event and the label on the form displays the record number.
 

Summary

 
Data Binding is a very powerful feature of .NET. Data binding which allows the user to access information from databases, arrays, or collections. The BindingManagerBase class is useful in the coordination of controls bound to the same data source. The BindingContext class is used to handle the BindingManagerBase objects. The object of the class helps in binding multiple data sources.