Hi fahimahmed,
I have created a sample please 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
<asp:DropDownList runat="server" ID="ddlCountry" AutoPostBack="true" OnSelectedIndexChanged="SelectedCountry">
</asp:DropDownList><br /><br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="ID" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</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.BindCountry();
this.BindCustomers();
}
}
private void BindCountry()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Customers WHERE Country IS NOT NULL", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.ddlCountry.DataValueField = "Country";
this.ddlCountry.DataTextField = "Country";
this.ddlCountry.DataSource = dt;
this.ddlCountry.DataBind();
this.ddlCountry.Items.Insert(0, new ListItem("ALL", ""));
}
}
}
}
private void BindCustomers()
{
string country = ddlCountry.SelectedValue;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT 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();
}
}
}
protected void SelectedCountry(object sender, EventArgs e)
{
this.BindCustomers();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindCountry()
Me.BindCustomers()
End If
End Sub
Private Sub BindCountry()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Country FROM Customers WHERE Country IS NOT NULL", 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.DataValueField = "Country"
Me.ddlCountry.DataTextField = "Country"
Me.ddlCountry.DataSource = dt
Me.ddlCountry.DataBind()
Me.ddlCountry.Items.Insert(0, New ListItem("ALL", ""))
End Using
End Using
End Using
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 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
Protected Sub SelectedCountry(ByVal sender As Object, ByVal e As EventArgs)
Me.BindCustomers()
End Sub
Screenshot