Hi kavithav,
I have used Northwind DataBase Customers table you can get the Northwind database from below article
Install the Northwind and Pubs Sample Databases in SQL Server Express
Refer below Sample and modify the code according to your need.
HTML
<div>
<asp:DropDownList ID="ddl1" runat="server" Height="22px" Width="121px" OnSelectedIndexChanged="ddl1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<br />
<asp:GridView ID="gvCustomers" runat="server" />
<div class="modal" style="display: none">
<div class="center">
<img alt="" src="loader.gif" />
</div>
</div>
</div>
<div>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(".modal").hide();
$("#ddl1").change(function () {
$(".modal").show();
});
});
</script>
</head>
</div>
C#
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
ddl1.DataSource = dt;
ddl1.DataTextField = "Country";
ddl1.DataValueField = "Country";
ddl1.DataBind();
}
}
}
}
}
}
private void BindGrid(string country)
{
string query = "SELECT ContactName, City, Country FROM Customers WHERE Country = @Country";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Country", country);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
Thread.Sleep(5000); // I have used Thread.Sleep just for displaying the loader for 5 Secs.
BindGrid(ddl1.SelectedItem.Text);
}
VB.Net
Private constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT DISTINCT Country FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
ddl1.DataSource = dt
ddl1.DataTextField = "Country"
ddl1.DataValueField = "Country"
ddl1.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Private Sub BindGrid(country As String)
Dim query As String = "SELECT ContactName, City, Country FROM Customers WHERE Country = @Country"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
cmd.Parameters.AddWithValue("@Country", country)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub ddl1_SelectedIndexChanged(sender As Object, e As EventArgs)
Thread.Sleep(5000)
' I have used Thread.Sleep just for displaying the loader for 5 Secs.
BindGrid(ddl1.SelectedItem.Text)
End Sub
ScreenShot