Hi akhter,
Check this sample.
HTML
Select Employee ID :
<asp:DropDownList ID="ddlEmployeeID" runat="server" AutoPostBack="true" OnSelectedIndexChanged="SelectedCustomer">
</asp:DropDownList><br /><br />
<asp:GridView ID="gvEmployees" runat="server">
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindEmployees();
this.BindDropdownID();
}
}
protected void SelectedCustomer(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,(FirstName+' ' +LastName) AS Name,City,Country FROM Employees WHERE EmployeeID=@EmployeeID", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmployeeID", ddlEmployeeID.SelectedValue);
con.Open();
this.gvEmployees.DataSource = cmd.ExecuteReader();
this.gvEmployees.DataBind();
con.Close();
}
}
}
private void BindDropdownID()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,(FirstName+' ' +LastName) AS Name FROM Employees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.ddlEmployeeID.DataTextField = "Name";
this.ddlEmployeeID.DataValueField = "EmployeeID";
this.ddlEmployeeID.DataSource = dt;
this.ddlEmployeeID.DataBind();
this.ddlEmployeeID.Items.Insert(0, new ListItem { Text = "--Select Customer--", Value = "" });
}
}
}
}
private void BindEmployees()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,(FirstName+' ' +LastName) AS Name,City,Country FROM Employees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindEmployees()
Me.BindDropdownID()
End If
End Sub
Protected Sub SelectedCustomer(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,(FirstName+' ' +LastName) AS Name,City,Country FROM Employees WHERE EmployeeID=@EmployeeID", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@EmployeeID", ddlEmployeeID.SelectedValue)
con.Open()
Me.gvEmployees.DataSource = cmd.ExecuteReader()
Me.gvEmployees.DataBind()
con.Close()
End Using
End Using
End Sub
Private Sub BindDropdownID()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,(FirstName+' ' +LastName) AS Name FROM Employees", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.ddlEmployeeID.DataTextField = "Name"
Me.ddlEmployeeID.DataValueField = "EmployeeID"
Me.ddlEmployeeID.DataSource = dt
Me.ddlEmployeeID.DataBind()
Me.ddlEmployeeID.Items.Insert(0, New ListItem With { .Text = "--Select Customer--", .Value = ""})
End Using
End Using
End Using
End Sub
Private Sub BindEmployees()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,(FirstName+' ' +LastName) AS Name,City,Country FROM Employees", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvEmployees.DataSource = dt
Me.gvEmployees.DataBind()
End Using
End Using
End Using
End Sub
Screenshot