Binding Data to Web Control in ADO.NET


In this article on Binding Data to Web Control in ADO.NET, I will explain how to bind the data values from a dataset to a web control such as List Controls.

The ToolBox in ASP.NET provides many data-bound controls. However, some of these controls are the basic data-bound controls whereas some advanced controls. evidently, the advanced data-bound controls have features of CRUD operations within the same control. Unlike these advanced controls, the basic controls don’t have all such features available within the control itself.

As far as the basic data-bound controls are concerned all List Controls and the AdRotator control fall in this category. While the controls like GridView, DataList, and DataGrid are some of the examples of advanced data-bound controls.

The List Controls

As can be seen, the List Controls have several properties that enable them to fetch and display data retrieved from a database table. The following list specifies some of these properties that are common to all of the List controls.

  • DataSource
  • DataTextField
  • DataValueField

In addition, the List Controls also have a method known as DataBind() that binds a data source to the corresponding server control such as a DropDown List.

Example of Binding Data to Web Control

The following example shows how to perform data binding using a DropDownList control. Basically, DropDownList is a server control in ASP.NET and represents a drop-down list. Further, it permits users to select a single item from the list.

Therefore, in the following example, we create a database with the name d1 in SQL Server and also create a table called Student. After that, we use the disconnected approach using Data Adapter to fetch the table data in a dataset. Further, that dataset acts as the data source for the drop-down list.

Finally, we set the DataSource property to the dataset object, DataTextField to the sname, and DataValueField to the s_id column of the table. Then, we call the DataBind() method of the DropDownList control.

As shown in the example, as soon as the user selects a particular value from the drop-down list, the SelectedIndexChanged event handler executes. Accordingly, the Labels get filled with the corresponding s_id and sname values.

Finally, the user will enter the marks in the TextBox and clicks on the Update Marks button. It will execute the update query.

using System;
using System.Data;
using System.Data.SqlClient;

namespace DataBindingExample1
    public partial class WebForm1 : System.Web.UI.Page
        SqlConnection cn;
        SqlDataAdapter da;
        DataSet ds;

        static int sid, marks;
        static string sname;
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                cn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersKAVITADocumentsd1.mdf;Integrated Security=True;Connect Timeout=30");
                da = new SqlDataAdapter("select * from Student", cn);
                ds = new DataSet();

                da.Fill(ds, "Student");
                DropDownList1.DataSource = ds;
                DropDownList1.DataTextField = "sname";
                DropDownList1.DataValueField = "s_id";

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            int x, x1;
            string s;
            x1 = DropDownList1.SelectedIndex;
            x = Int32.Parse(DropDownList1.SelectedItem.Value);
            s = DropDownList1.SelectedItem.Text;

            Label1.Text = x.ToString();
            Label2.Text = s;

            sid = x;
            sname = s;

        protected void Button1_Click(object sender, EventArgs e)
            marks = Int32.Parse(TextBox1.Text);
            //Response.Write(sid + " " + sname + " " + marks);

            String str = "update Student set marks = '"+marks+"' where s_id = "+sid;
            cn = new SqlConnection(@"Data Source=(LocalDB)MSSQLLocalDB;AttachDbFilename=C:UsersKAVITADocumentsd1.mdf;Integrated Security=True;Connect Timeout=30");
            SqlCommand cmd = new SqlCommand(str, cn);

            int n = cmd.ExecuteNonQuery();
            if (n > 0)
                Response.Write("Marks Updated!");
                Response.Write("Some Error has Occurred!");


Example of Binding Data to Web Control in ADO.NET
Example of Binding Data to Web Control in ADO.NET

Updated Database

Database Table after Update
Database Table after Update

Similarly, an example of Data Binding using Bulleted List Control can be found here.


This article illustrates Binding Data to Web Control using a SQL Server database as the data source. Further, an example of Data Binding to a DropDownList control is also demonstrated here.

Further Reading

Parameter and ParameterCollection in ADO.NET

Database Manipulation Using DataGrid

Example of Button and Link Button Control in ASP.NET

Example of Chart Control in ASP.NET

Creating a DataTable from a DataReader in ASP.NET

Deleting a Record using DataGrid Control in ASP.NET

Edit a Record Using DataGrid Control in ASP.NET

Insert a Record Using ItemCommand Event in DataGrid

CRUD Operations with DataGrid in ASP.NET

Creating Columns in a DataGrid Control

XML Documents and DataSet in ASP.NET

Code Render Block in ASP.NET

ASP.NET Core Features and Advantages

Display Images Using DataList Control

Adding Images Using Image Control

Creating a Group of Radio Buttons Using RadioButtonList Control

Example of Button Control in ASP.NET

Using MD5 Hash Algorithm

ItemDataBound Event in DataList

More Features of DataList in ASP.NET

A Simple Example of Using a DataList Control in ASP.NET

Properties and Methods of DataList Control in ASP.NET

ASP.NET Practice Exercise

Exploring DataList Control in ASP.NET

Custom Validator Control in ASP.NET

Validation Summary Control in ASP.NET

Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator

An Example of Data Binding with RadioButtonList Control

Binding Data to Web Control in ADO.NET

Examples of AdRotator Control in ASP.NET

Examples of Validation Controls in ASP.NET

Overview of MVC architecture in ASP.NET


You may also like...