inchirags@gmail.com Chirag's ASP.NET C# Tutorial https://www.chirags.in
*****************************************************************************************
* Country, State and City dropdown list using Database in ASP.NET C# *
*****************************************************************************************
/******************************create table code start here**********/
/****** Object: Table [dbo].[Country] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Country](
[CountryId] [int] NOT NULL,
[Country] [varchar](30) NULL,
[is_active] [int] NULL,
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
/****** Object: Table [dbo].[countryState] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[countryState](
[StateId] [int] NOT NULL,
[CountryId] [int] NULL,
[State] [varchar](30) NULL,
[is_active] [int] NULL,
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].[countryState] WITH CHECK ADD FOREIGN KEY([CountryId])
REFERENCES [dbo].[Country] ([CountryId])
GO
/****** Object: Table [dbo].[stateCity] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[stateCity](
[CityId] [int] NULL,
[StateId] [int] NULL,
[City] [varchar](30) NULL,
[is_active] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[stateCity] WITH CHECK ADD FOREIGN KEY([StateId])
REFERENCES [dbo].[countryState] ([StateId])
GO
/************************* .aspx page coding *********************************/
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div class="form-group">
<label class="control-label col-sm-3" for="address">Country :</label>
<div class="col-sm-9">
<asp:UpdatePanel ID="countrypanel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlcountry" AutoPostBack="true" AppendDataBoundItems="true" runat="server" onselectedindexchanged="ddlcountry_SelectedIndexChanged" class="form-control"></asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlcountry" /></Triggers>
</asp:UpdatePanel>
</div>
</div>
<div class="clearfix"> </div>
<div class="form-group">
<label class="control-label col-sm-3" for="address">State :</label>
<div class="col-sm-9">
<asp:UpdatePanel ID="statepanel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlstate" AutoPostBack="true" AppendDataBoundItems="true" runat="server" onselectedindexchanged="ddlstate_SelectedIndexChanged" class="form-control"></asp:DropDownList>
</ContentTemplate>
<Triggers><asp:AsyncPostBackTrigger ControlID ="ddlstate"/></Triggers>
</asp:UpdatePanel>
</div>
</div>
<div class="clearfix"> </div>
<div class="form-group">
<label class="control-label col-sm-3" for="address">City :</label>
<div class="col-sm-9">
<asp:UpdatePanel ID="citypanel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlcity" AutoPostBack="true" AppendDataBoundItems="true" runat="server" class="form-control"></asp:DropDownList>
</ContentTemplate>
<Triggers><asp:AsyncPostBackTrigger ControlID ="ddlcity"/></Triggers>
</asp:UpdatePanel>
</div>
</div>
/******************* .cs page coding *******************************/
public void Bind_ddlCountry()
{
conn.Open();
SqlCommand cmd = new SqlCommand("select Country,CountryId from Country where is_active=1", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddlcountry.DataSource = dr;
ddlcountry.Items.Clear();
ddlcountry.Items.Add("--Please Select country--");
ddlcountry.DataTextField = "Country";
ddlcountry.DataValueField = "CountryId";
ddlcountry.DataBind();
conn.Close();
}
public void Bind_ddlState()
{
conn.Open();
SqlCommand cmd = new SqlCommand("select State,StateID from countryState where is_active=1 and CountryId='" + ddlcountry.SelectedValue + "'", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddlstate.DataSource = dr;
ddlstate.Items.Clear();
ddlstate.Items.Add("--Please Select state--");
ddlstate.DataTextField = "State";
ddlstate.DataValueField = "StateID";
ddlstate.DataBind();
conn.Close();
}
public void Bind_ddlCity()
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from stateCity where is_active=1 and StateId ='" + ddlstate.SelectedValue + "'", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddlcity.DataSource = dr;
ddlcity.Items.Clear();
ddlcity.Items.Add("--Please Select city--");
ddlcity.DataTextField = "City";
ddlcity.DataValueField = "CityID";
ddlcity.DataBind();
conn.Close();
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
Bind_ddlState();
}
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
Bind_ddlCity();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_ddlCountry();
}
}
Note : Flow the Process shown in video.
Subscribe and like for more videos:
https://www.youtube.com/@chiragstutorial
Don't forget to, Follow, Like, Share &, Comment
Thanks & Regards,
Chitt Ranjan Mahto "Chirag"
_________________________________________________________________________________________
Note: All scripts used in this demo will be available in our website.
Link will be available in description.