Hi Kankon,
Please refer below sample.
Database
USE [master]
GO
CREATE DATABASE [Cascading_ddl]
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Cascading_ddl].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [Cascading_ddl] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Cascading_ddl] SET ANSI_NULLS OFF
GO
ALTER DATABASE [Cascading_ddl] SET ANSI_PADDING OFF
GO
ALTER DATABASE [Cascading_ddl] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [Cascading_ddl] SET ARITHABORT OFF
GO
ALTER DATABASE [Cascading_ddl] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [Cascading_ddl] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [Cascading_ddl] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [Cascading_ddl] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [Cascading_ddl] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [Cascading_ddl] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [Cascading_ddl] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [Cascading_ddl] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [Cascading_ddl] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [Cascading_ddl] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [Cascading_ddl] SET DISABLE_BROKER
GO
ALTER DATABASE [Cascading_ddl] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [Cascading_ddl] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [Cascading_ddl] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [Cascading_ddl] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [Cascading_ddl] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [Cascading_ddl] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [Cascading_ddl] SET RECOVERY SIMPLE
GO
ALTER DATABASE [Cascading_ddl] SET MULTI_USER
GO
ALTER DATABASE [Cascading_ddl] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [Cascading_ddl] SET DB_CHAINING OFF
GO
ALTER DATABASE [Cascading_ddl] SET READ_WRITE
GO
USE [Cascading_ddl]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Countries](
[CountryId] [int] NOT NULL,
[CountryName] [varchar](100) NOT NULL,
CONSTRAINT [PK_Countries] PRIMARY KEY CLUSTERED
(
[CountryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[States](
[StateId] [int] NOT NULL,
[CountryId] [int] NOT NULL,
[StateName] [varchar](100) NOT NULL,
CONSTRAINT [PK_States] PRIMARY KEY CLUSTERED
(
[StateId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[States] WITH CHECK ADD CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryId])
REFERENCES [dbo].[Countries] ([CountryId])
GO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Cities](
[CityId] [int] NOT NULL,
[StateId] [int] NOT NULL,
[CityName] [varchar](100) NOT NULL,
CONSTRAINT [PK_Cities] PRIMARY KEY CLUSTERED
(
[CityId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Cities] WITH CHECK ADD CONSTRAINT [FK_Cities_States] FOREIGN KEY([StateId])
REFERENCES [dbo].[States] ([StateId])
GO
ALTER TABLE [dbo].[Cities] CHECK CONSTRAINT [FK_Cities_States]
GO
insert into Countries
select 1,'USA'
union all
select 2,'India'
union all
select 3,'Canada'
go
insert into States
select 1,1,'Alabama'
union all
select 2,1,'Arizona'
union all
select 3,1,'Alaska'
union all
select 4,2,'Maharashtra'
union all
select 5,2,'Gujarat'
union all
select 6,2,'Goa'
union all
select 7,3,'Ontario'
union all
select 8,3,'Quebec'
union all
select 9,3,'Manitoba'
go
insert into Cities
select 1,1,'Abbeville'
union all
select 2,1,'Argo'
union all
select 3,2,'Buckeye'
union all
select 4,2,'Carefree'
union all
select 5,3,'Juneau'
union all
select 6,3,'Sitka'
union all
select 7,4,'Mumbai'
union all
select 8,4,'Pune'
union all
select 9,5,'Ahmedabad'
union all
select 10,5,'Gandhinagar'
union all
select 11,6,'Panjim'
union all
select 12,6,'Vasco'
union all
select 13,7,'Ottawa'
union all
select 14,7,'Port Hope'
union all
select 15,8,'Chandler'
union all
select 16,8,'Princeville'
union all
select 17,9,'Carman'
union all
select 18,9,'Roblin'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customer](
[id] [int] IDENTITY(1,1) NOT NULL,
[Countries] [nvarchar](max) NULL,
[States] [nvarchar](max) NULL,
[Cities] [nvarchar](max) NULL,
[CountryId] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Customer] ON
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (1, N'India', N'Maharashtra', N'Pune', N'1')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (2, N'India', N'Goa', N'Lucknow', N'3')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (3, N'USA', N'Alabama', N'Abbeville', N'8')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (4, N'USA', N'Arizona', N'Buckeye', N'6')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (5, N'USA', N'Alaska', N'Juneau', N'7')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (6, N'Canada', N'Ontario', N'Princeville', N'6')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (7, N'Canada', N'Quebec', N'Vasco', N'4')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (8, N'India', N'Maharashtra', N'Mumbai', N'5')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (9, N'India', N'Maharashtra', N'Mumbai', N'5')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (10, N'Canada', N'Manitoba', N'Carman', N'6')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (11, N'India', N'Gujrat', N'panaji', N'7')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (12, N'India', N'Maharashtra', N'Mumbai', NULL)
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (13, N'Canada', N'Manitoba', N'Chandler', NULL)
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (14, N'India', N'Maharashtra', N'Mumbai', N'3')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (15, N'USA', N'Alabama', N'Juneau', N'5')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (16, N'USA', N'Arizona', N'Sitka', N'5')
GO
INSERT [dbo].[Customer] ([id], [Countries], [States], [Cities], [CountryId]) VALUES (17, N'India', N'Maharashtra', N'Pune', NULL)
GO
SET IDENTITY_INSERT [dbo].[Customer] OFF
GO
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table dir="rtl">
<tr>
<td style="border: medium solid #000000">القطاع<br />
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Changed">
</asp:DropDownList>
</strong></td>
<td style="border: medium solid #000000">الادارة<strong><br />
<asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="true" OnSelectedIndexChanged="State_Changed">
</asp:DropDownList>
</strong></td>
<td style="border: medium solid #000000">القسم<strong><br />
<asp:DropDownList ID="ddlCities" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Cities_Changed">
</asp:DropDownList>
</strong>
</td>
</tr>
</table>
<hr />
<asp:GridView runat="server" ID="GridView1"></asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string query = "select CountryId,CountryName from Countries";
this.BindDropDownList(ddlCountries, query, "CountryName", "CountryId", "Select Country");
ddlStates.Enabled = false;
ddlCities.Enabled = false;
ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
GridView1.DataSource = null;
GridView1.DataBind();
}
}
protected void Country_Changed(object sender, EventArgs e)
{
ddlStates.Enabled = false;
ddlCities.Enabled = false;
ddlStates.Items.Clear();
ddlCities.Items.Clear();
ddlStates.Items.Insert(0, new ListItem("Select State", "0"));
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
int countryId = int.Parse(ddlCountries.SelectedItem.Value);
if (countryId > 0)
{
string query = string.Format("select StateId, StateName from States where CountryId = {0}", countryId);
this.BindDropDownList(ddlStates, query, "StateName", "StateId", "Select State");
ddlStates.Enabled = true;
}
GridView1.DataSource = null;
GridView1.DataBind();
}
protected void State_Changed(object sender, EventArgs e)
{
ddlCities.Enabled = false;
ddlCities.Items.Clear();
ddlCities.Items.Insert(0, new ListItem("Select City", "0"));
int stateId = int.Parse(ddlStates.SelectedItem.Value);
if (stateId > 0)
{
string query = string.Format("select CityId, CityName from Cities where StateId = {0}", stateId);
this.BindDropDownList(ddlCities, query, "CityName", "CityId", "Select City");
ddlCities.Enabled = true;
}
GridView1.DataSource = null;
GridView1.DataBind();
}
protected void Cities_Changed(object sender, EventArgs e)
{
this.BindGridView();
}
private void BindGridView()
{
string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT * FROM Customer WHERE Cities = @Cities";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Cities", ddlCities.SelectedItem.Text);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}
Screenshot