In this article I will explain with an example, how to bind Label and TextBox on DropDownList change in ASP.Net using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Namespaces
You will need to import the following namespaces.
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
HTML Markup
The following HTML Markup consists of:
DropDownList – For displaying ID of the Customers.
Properties
AutoPostBack – For allowing controls submit the Form (PostBack) which cannot submit on their own.
Events
The DropDownList has been assigned with an OnSelectedIndexChanged event handler.
TextBox – For displaying Name of the selected CustomerId.
Label – For displaying Country name of the selected CustomerId.
<b>Select Customer ID :</b>
<asp:DropDownList ID="ddlCustomers" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
</asp:DropDownList>
<br />
<p><b>Customer Details</b></p>
<hr/>
<table>
<tr>
<td>Name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Country:<asp:Label ID="lblCountry" runat="server" Text=""></asp:Label></td>
</tr>
</table>
Populating DropDownList from database
Inside the Page Load event handler, the DropDownList is populated with records i.e. ID fetched from the SQL Server database.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT CustomerID FROM Customers";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
ddlCustomers.DataSource = cmd.ExecuteReader();
ddlCustomers.DataTextField = "CustomerID";
ddlCustomers.DataValueField = "CustomerID";
ddlCustomers.DataBind();
con.Close();
ddlCustomers.Items.Insert(0, new ListItem("--Select Customer--", ""));
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT CustomerID FROM Customers"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
ddlCustomers.DataSource = cmd.ExecuteReader()
ddlCustomers.DataTextField = "CustomerID"
ddlCustomers.dataValueField = "CustomerID"
ddlCustomers.DataBind()
con.Close()
ddlCustomers.Items.Insert(0, New ListItem("--Select Customer--", ""))
End Using
End Using
End If
End Sub
Setting TextBox and Label OnSelectedIndexChanged
Inside the OnSelectedIndexChanged event handler, the selected ID is fetched from the DropDownList and passed as parameter to SQL query..
The records i.e. Name and Country are fetched from the Customers Table using ExecuteReader method and set to the respective controls.
C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
string customerId = ddlCustomers.SelectedItem.Value;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Name, Country FROM Customers WHERE CustomerID = @CustomerID";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@CustomerID", customerId);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
txtName.Text = sdr["Name"].ToString();
lblCountry.Text = sdr["Country"].ToString();
}
con.Close();
}
}
}
VB.Net
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim customerId As String = ddlCustomers.SelectedItem.Value
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT Name, Country FROM Customers WHERE CustomerID = @CustomerID"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@CustomerID", customerId)
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read() Then
txtName.Text = sdr("Name").ToString()
lblCountry.Text = sdr("Country").ToString()
End If
con.Close()
End Using
End Using
End Sub
Screenshot
Demo
Download