296 views
asked in PostgreSQL by

Country, State and City dropdown list using Database in ASP.NET C#

1 Answer

answered by

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">&nbsp;</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">&nbsp;</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.

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...