1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Data Binding in ASP.NET

Discussion in 'ASP.NET' started by MinalS, Aug 18, 2015.

  1. User can bind the data with the controls of the forms. This process is known as data binding. There are two types of data binding in ASP.NET known as simple data binding and declarative data binding.

    Simple data binding

    In simple data binding, the control is bounded to a data set. The properties of the control are used for binding with the value. Depending on the control to be bounded, the binding’s property is set.

    Consider the following example where the Academic information of the student is bounded using various controls.

    Example to demonstrate the simple data binding in ASP.NET

    Consider an example where a windows form is used for displaying the details.
    1. Create a Windows form application in Visual studio.
    2. Add the windows form to the design view.
    3. Add three labels and corresponding textboxes to it. Add the labels as Name, Age and Location.

    4. Select the View, Properties Window in the application
    5. Select the first textbox and navigate to the properties window.
    6. Expand the DataBindings property.
    7. From the drop down list, select the Text property.
    8. Click on the Add Project Data Source from the drop down list.
    9. Add a connection to the database and select the appropriate table.
    10. Select Other Data Sources, Project data source, DataSet.
    11. Select the appropriate value and bind the textbox control with it.
    12. Press F5 or select Start debugging option. Execute the windows form and the following output is displayed.
    Declarative data binding

    The process of binding a component like listbox, DataGrid, record list with the dataset is known as declarative binding. When there is more than one element in the database, the declarative binding is used.

    Some of the controls used for the declarative data binding are listed below.
    1. DataGrid: The data from multiple records is displayed using the DataGrid view. The DataSource property of the control is used for binding the specific element data.
    2. ListBox: The data for a column from different dataset is displayed. The DataSource property is used for binding the control. The control binds to the specific element using the DisplayMember property.
    3. ComboBox: The DisplayMember property is used for binding the control to the specific data element. The DataSource property is used for binding the control to the data source.
    The following objects are needed for data binding in ASP.NET.
    1. The data accessed from the database is stored in the dataset.
    2. The data provider is used for accessing data through the command object
    3. The data adapter is used for selecting, updating, inserting, deleting the data using commands.
    Consider the following example to demonstrate the declarative data binding
    1. Create an ASP.NET web application in visual studio.
    2. Add a grid view control in the design view of the application
    3. In the source view, add the following code

      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <form id="form1" runat="server">
          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
          DataSourceID="SqlDataSource1" >
          <asp:BoundField DataField="studid" HeaderText="studid" SortExpression="studid" />
          <asp:BoundField DataField="studname" HeaderText="studname" SortExpression="studname" />
          <asp:BoundField DataField="marks" HeaderText="marks" SortExpression="marks"/>
          <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:demoConnectionString1 %>"
          SelectCommand="SELECT * FROM [ result ] “> </asp:SqlDataSource>
    4. The above code is compiled and executed, the following output is generated.


Share This Page