Hi rajeshg12,
Check this sample. now take its reference.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Select Country :
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="SelectedCountry">
</asp:DropDownList><br /><br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Customer ID">
<ItemTemplate>
<asp:Label ID="lblCustomerID" Text='<%# Eval("CustomerID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Eval("ContactName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" Text='<%# Eval("City") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" Text='<%# Eval("Country") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</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.BindCountries();
this.BindCustomers();
}
}
protected void SelectedCountry(object sender, EventArgs e)
{
this.BindCustomers();
}
private void BindCustomers()
{
string country = ddlCountry.SelectedValue;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 CustomerID,ContactName,City,Country FROM Customers WHERE Country=@Country OR @Country IS NULL", con))
{
cmd.CommandType = CommandType.Text;
if (!string.IsNullOrEmpty(country))
{
cmd.Parameters.AddWithValue("@Country", country);
}
else
{
cmd.Parameters.AddWithValue("@Country", (object)DBNull.Value);
}
con.Open();
this.gvCustomers.DataSource = cmd.ExecuteReader();
this.gvCustomers.DataBind();
con.Close();
}
}
}
private void BindCountries()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Customers WHERE Country != ''", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.ddlCountry.DataTextField = "Country";
this.ddlCountry.DataValueField = "Country";
this.ddlCountry.DataSource = dt;
this.ddlCountry.DataBind();
this.ddlCountry.Items.Insert(0, new ListItem { Text = "All Country", Value = "" });
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindCountries()
Me.BindCustomers()
End If
End Sub
Protected Sub SelectedCountry(ByVal sender As Object, ByVal e As EventArgs)
Me.BindCustomers()
End Sub
Private Sub BindCustomers()
Dim country As String = ddlCountry.SelectedValue
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 10 CustomerID,ContactName,City,Country FROM Customers WHERE Country=@Country OR @Country IS NULL", con)
cmd.CommandType = CommandType.Text
If Not String.IsNullOrEmpty(country) Then
cmd.Parameters.AddWithValue("@Country", country)
Else
cmd.Parameters.AddWithValue("@Country", CObj(DBNull.Value))
End If
con.Open()
Me.gvCustomers.DataSource = cmd.ExecuteReader()
Me.gvCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Private Sub BindCountries()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Country FROM Customers WHERE Country != ''", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.ddlCountry.DataTextField = "Country"
Me.ddlCountry.DataValueField = "Country"
Me.ddlCountry.DataSource = dt
Me.ddlCountry.DataBind()
Me.ddlCountry.Items.Insert(0, New ListItem With {.Text = "All Country",.Value = ""})
End Using
End Using
End Using
End Sub
Screenshot
![](https://i.imgur.com/jYE1Ktp.gif)