In this article I will explain with an example, how to populate
TextBox based on
DropDownList selection in
ASP.Net using C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
HTML Markup
The
HTML Markup consists of:
DropDownList – For selecting Customer ID.
The
DropDownList has been assigned
OnSelectedIndexChanged event and also the
AutoPostBack property is set to True.
TextBox – For display city.
Label – For display country.
<form id="form1" runat="server">
<div>
Select Customer ID
<asp:DropDownList ID="ddlCustomers" runat="server" AutoPostBack = "true"
OnSelectedIndexChanged="ddlCustomers_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<br />
<p>---Customer Details---</p>
<hr />
City :
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<br />
Country :
<asp:Label ID="lblCountry" runat="server" Text=""></asp:Label>
</div>
</form>
Populating DropDownList from Database
Inside the
Page Load event of the page, the
DropDownList is populated with records from the
Customers table of the Northwind database.
Here Select Query is executed and it gets the CustomerID and the ContactName fields.
Then
CustomerID field is assigned to the
DataValueField property of the
DropDownList and the
ContactName field is assigned to the
DataTextField property.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ddlCustomers.Items.Add(new ListItem("--Select Customer--",""));
ddlCustomers.AppendDataBoundItems = true;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sql = "select CustomerID, ContactName from Customers";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
try
{
con.Open();
ddlCustomers.DataSource = cmd.ExecuteReader();
ddlCustomers.DataTextField = "ContactName";
ddlCustomers.DataValueField = "CustomerID";
ddlCustomers.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
ddlCustomers.Items.Add(New ListItem("--Select Customer--", ""))
ddlCustomers.AppendDataBoundItems = True
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sql As String = "select CustomerID, ContactName from Customers"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(sql, con)
Try
con.Open()
ddlCustomers.DataSource = cmd.ExecuteReader()
ddlCustomers.DataTextField = "ContactName"
ddlCustomers.DataValueField = "CustomerID"
ddlCustomers.DataBind()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Using
End Using
End If
End Sub
Populating TextBox and Label controls on DropDownList selection
Inside the OnSelectedIndexChanged event handler, the City and Country fields are fetched based on the selected CustomerID value.
Then fetched values are displayed in the
TextBox and
Label controls respectively.
C#
protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sql = "select City, Country from Customers where CustomerID = @CustomerID";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddWithValue("@CustomerID", ddlCustomers.SelectedItem.Value);
try
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
txtCity.Text = sdr[0].ToString();
lblCountry.Text = sdr["Country"].ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
}
VB.Net
Protected Sub ddlCustomers_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sql As String = "select City, Country from Customers where CustomerID = @CustomerID"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@CustomerID", ddlCustomers.SelectedItem.Value)
Try
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
txtCity.Text = sdr(0).ToString()
lblCountry.Text = sdr("Country").ToString()
End While
End Using
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Using
End Using
End Sub
Screenshot
Demo
Downloads