I am selecting the Address from Table based on Selected Value of DropDownList. Value here is the Id of table.
HTML:
Select Name
<asp:DropDownList ID="ddlEmployees" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged_ddlEmployees">
</asp:DropDownList>
<br />
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" />
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateDropDownList();
}
}
private void PopulateDropDownList()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT FirstName,EmployeeID FROM Employees", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
this.ddlEmployees.DataTextField = "FirstName";
this.ddlEmployees.DataValueField = "EmployeeID";
this.ddlEmployees.DataSource = ds;
this.ddlEmployees.DataBind();
this.ddlEmployees.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Please Select", "0"));
}
}
}
}
protected void OnSelectedIndexChanged_ddlEmployees(object sender, EventArgs e)
{
if (this.ddlEmployees.SelectedItem.Value != "0")
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Address FROM Employees WHERE EmployeeID = @EmployeeID", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@EmployeeID", this.ddlEmployees.SelectedItem.Value);
con.Open();
object address = cmd.ExecuteScalar();
con.Close();
this.txtAddress.Text = address.ToString();
}
}
}
}
else
{
this.txtAddress.Text = "Please Select Name from List";
}
}
VB:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateDropDownList()
End If
End Sub
Private Sub PopulateDropDownList()
Dim constr As String = ConfigurationManager.ConnectionStrings("conString2").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT FirstName,EmployeeID FROM Employees", con)
Using da As New SqlDataAdapter(cmd)
con.Open()
Dim ds As New DataSet()
da.Fill(ds)
Me.ddlEmployees.DataTextField = "FirstName"
Me.ddlEmployees.DataValueField = "EmployeeID"
Me.ddlEmployees.DataSource = ds
Me.ddlEmployees.DataBind()
Me.ddlEmployees.Items.Insert(0, New System.Web.UI.WebControls.ListItem("Please Select", "0"))
End Using
End Using
End Using
End Sub
Protected Sub OnSelectedIndexChanged_ddlEmployees(sender As Object, e As EventArgs)
If Me.ddlEmployees.SelectedItem.Value <> "0" Then
Dim constr As String = ConfigurationManager.ConnectionStrings("conString2").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT Address FROM Employees WHERE EmployeeID = @EmployeeID", con)
Using da As New SqlDataAdapter(cmd)
cmd.Parameters.AddWithValue("@EmployeeID", Me.ddlEmployees.SelectedItem.Value)
con.Open()
Dim address As Object = cmd.ExecuteScalar()
con.Close()
Me.txtAddress.Text = address.ToString()
End Using
End Using
End Using
Else
Me.txtAddress.Text = "Please Select Name from List"
End If
End Sub
Import Namespace:
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data
Image:
Thank You.