Sunday, 27 August 2017

Understanding Data Binding

Understanding Data Binding
ASP.NET provides a rich set of controls that enable you to display information to users as well as accept information from users. You can display information in controls from a wide variety of data stores, such as properties, arrays, data structures, or databases. Some of the data stores are static, whereas others are dynamic. You usually use static data stores to display information for user reference. In addition to displaying static information in controls, there are situations that require you to display information dynamically. For example, you might need to display the discount based on the quantity purchased for a product. Also, you might need a control to display information from a database whose data keeps changing constantly. In such situations, ASP.NET provides a solution by providing a feature that allows data binding to controls.

Introduction to Data Binding
Data binding means binding controls to information stored in a data store. Here, the term "data" is used in a very broad sense. When we talk about data binding, it implies binding any control property to almost any kind of data store. A data store can be as simple as a public property on a page, or as complex as a database stored on a server. This broad choice among data stores provides high flexibility, and thus enables you to bind a control to any data store based on your need. The Web Forms controls that are bound to a data store access data through the properties of specific classes, categorized as data classes. Data classes typically include methods that can be used for updating the underlying data stores. Because the term "data" is used in a broad sense, the class category "data classes" is also used in a generic, broad sense. These classes differ depending on the data store. Some data classes provide more functionality than others — you can use any one of these classes depending on your need. You can bind a control to different data stores, such as properties, methods, or collections. These different data stores can be bound to a control property by using databinding expressions. While binding, the data is always bound to a particular property of the control (the property name might differ for various controls). When a data binding expression is evaluated, the resulting value is loaded in the control's bound property. You can bind simple controls to public properties. A public property can be of a control on a page or the page itself.

 When you bind a control property to a data store, the Web Forms Framework cannot evaluate data binding expressions automatically. To display the evaluated value in the control's bound property, you need to call the DataBind() method explicitly. The page and each control on the page support this method. When you call the DataBind() method for a control, it is cascaded to all its child controls. For example, if you call the DataBind() method for the page control, the method is automatically called for all the controls on the page. When you need to display data in the bound controls as soon as the page is loaded. In such a situation, you can call the DataBind() method at the Page_Load stage. You can call the DataBind() method for the page or for a specific control, depending on your requirement. When data in the dataset is updated and you want to display the updated data in the bound controls. In such a situation, you need to call the DataBind() method in the event -handling methods that resulted in the change to the dataset. Again, you can call the DataBind() method for the page or for specific controls depending on whether you want to refresh the complete page or specific controls.

Using SQL Server with ASP.NET
With more and more applications shifting to the Internet, e-business is booming. To conduct business on the Internet, Web applications need to access data stored on a server. Thus, data access is most critical to real-world applications. Visual Studio .NET provides Web Forms controls, such as the DataGrid control that you can use to access data from various data sources, such as a SQL server or a Jet database.

Introduction to Server-Side Data Access from a SQL Server
Server-side data access is critical to all real-world applications. Therefore, these applications must address server-side data access to implement business solutions. This section introduces you to the SQL server data access through Web Forms. Microsoft SQL Server is a Relational Database Management System (RDBMS) that is used to store and organize related data — the collection of related data is called a database. Microsoft SQL Server is based on the client/server architecture, in which data is stored on a centralized computer called a server. Other computers, called clients, can access the data stored on the server through a network. The client/server architecture prevents data inconsistency. You can access data stored on a SQL server through Web Forms. To do so, you can create Web applications that have data access controls. These data access Web controls present the data in a consistent manner irrespective of the actual source, such as Microsoft SQL Server or MS Access. Therefore, while creating a Web application, you do not need to worry about the format of the data. However, before you can access or manipulate data from a SQL server, you need to perform the following steps in the specified sequence:

1. Establish a connection with the SQL Server.
2. Write the actual command to access or manipulate data.
 3. Create a result set of the data from the data source with which the application can work. This result set is called the data set and is disconnected from the actual source. The application accesses and updates data in the data set, which is later reconciled with the actual data source.
To achieve this functionality, you first need to import two namespaces, System.Data and System.Data.SqlClient, into your Web Forms page. You can use the different classes to create and manipulate database objects. The syntax is given as follows:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

The two namespaces are described as follows:
System.Data: A collection of classes that are based on the ADO.NET architecture. The ADO.NET architecture allows for efficient data management and manipulation from multiple data sources. ADO.NET provides tools to request and update data in a data set, and reconcile data in the actual data source. Some of the classes included in this namespace are described as follows:
DataSet: Represents the data set cached in memory with which applications work.
DataTable: Represents a table of data in a data set.
DataRow: Represents a row of data in a data table.
DataColumn: Represents a column of data in a data table.

System.Data.SqlClient: A collection of classes that are used to access SQL server data sources. Some of the classes are listed as follows:
SqlConnection: Represents a connection with a SQL server data source. The first step to      .access data from a SQL server database is to create an object of this class
SqlDataAdepter: Represents a set of data commands and database connections that are used to   access or manipulate data. After creating a SqlConnection object, you need to create an object of the SqlDataAdapter class to populate the data set and update the data source.
SqlCommand: Represents the SQL command toperform data operations in a SQL server data source.

You use the following code to import the two namespaces if you want to use the Visual Basic code file instead of the ASPX file:

Imports System.Data;

0 comments

Post a Comment