In most of the registration forms you can find a common thing; there will be 3 drop-down lists asking you to choose your country, state, and city as below.

Address information

The specialty of these drop-down lists is when you select a country from the first drop down list, it should show all the states of the corresponding country in the second drop-down list, and if you select a state from the second drop-down list, in the thirs drop-down list it should show all the city names of the corresponding state dynamically.

Let us take an example for better understanding.

Select State
Figure 1

Select City

Figure 2

Have a look at the above screen shots. In pic1, when I select the country as India, in the second drop-down list it shows all the states of India. In pic2 when I select the state as Odisha it shows all the cities of Odisha.

Lets learn step by step how to design it.

  • First we have to design our database.
  • Now create a database with name “CsharpCorner”.
  • Database: CsharpCorner

Now create a table to all the country names:

Table: tbl_country

create a table

Enter some data with in it like:

Country

Now design the 2nd table “tbl_state” like this:

Design View

And enter some data.

enter some data

Now design the 3rd Table like this:

Table design

And enter some sample data like this:

enter some sample data

Now table creation is completed, establish a primary key and foreign key relationship between the tables.

tables

Now the database part is completed. Let us write code for it,

  • 1st create a ASP.NET empty project name it as “DynamicDataBindingWithDropDownList”.
  • Add a webform in it with name “DinamicDDL” and write the following code inside it.

Design View: (DynamicDDL.aspx)

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicDDL.aspx.cs" Inherits="DynamicDataBindingWithDropDownList.DynamicDDL" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div align="center">
  10. <table>
  11. <tr>
  12. <th width="18%">Country</th>
  13. <th width="3%">:</th>
  14. <td>
  15. <asp:DropDownList ID="ddlCountry" runat="server" Width="185px" Height="30px" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" >
  16. <asp:ListItem>--Select--</asp:ListItem>
  17. <asp:ListItem>India</asp:ListItem>
  18. <asp:ListItem>USA</asp:ListItem>
  19. <asp:ListItem>China</asp:ListItem>
  20. <asp:ListItem>Japan</asp:ListItem>
  21. </asp:DropDownList>
  22. </td>
  23. </tr>
  24. <tr>
  25. <th>State</th>
  26. <th>:</th>
  27. <td>
  28. <asp:DropDownList ID="ddlState" runat="server" Width="185px" Height="30px" AutoPostBack="true" OnSelectedIndexChanged="ddlState_SelectedIndexChanged" />
  29. </td>
  30. </tr>
  31. <tr>
  32. <th>City</th>
  33. <th>:</th>
  34. <td>
  35. <asp:DropDownList ID="ddlCity" runat="server" Width="185px" Height="30px" ></asp:DropDownList>
  36. </td>
  37. </tr>
  38. </table>
  39. </div>
  40. </form>
  41. </body>
  42. </html>
Code View: (DynamicDDL.aspx.cs)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. namespace DynamicDataBindingWithDropDownList
  11. {
  12. public partial class DynamicDDL : System.Web.UI.Page
  13. {
  14. SqlConnection con = null;
  15. SqlCommand cmd = null;
  16. SqlDataReader dr = null;
  17. string strSqlCmd = string.Empty;
  18. protected void Page_Load(object sender, EventArgs e)
  19. {
  20. con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConStr"].ConnectionString);
  21. }
  22. protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
  23. {
  24. strSqlCmd = "select * from tbl_state where countryid=" + ddlCountry.SelectedIndex;
  25. SqlCommand cmd = new SqlCommand(strSqlCmd,con);
  26. if(con.State!=ConnectionState.Open)
  27. {
  28. con.Open();
  29. }
  30. dr = cmd.ExecuteReader();
  31. ddlState.DataSource = dr;
  32. ddlState.DataTextField = "state";
  33. ddlState.DataValueField = "id";
  34. ddlState.DataBind();
  35. ddlState.Items.Insert(0, new ListItem("-Select-", "0"));
  36. if (con.State != ConnectionState.Closed)
  37. con.Close();
  38. }
  39. protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
  40. {
  41. strSqlCmd = "select * from tbl_city where stateid=" + ddlState.SelectedValue;
  42. SqlCommand cmd = new SqlCommand(strSqlCmd, con);
  43. if (con.State != ConnectionState.Open)
  44. {
  45. con.Open();
  46. }
  47. dr = cmd.ExecuteReader();
  48. ddlCity.DataSource = dr;
  49. ddlCity.DataTextField = "city";
  50. ddlCity.DataBind();
  51. ddlCity.Items.Insert(0, new ListItem("-Select-", "0"));
  52. if (con.State != ConnectionState.Closed)
  53. con.Close();
  54. }
  55. }
  56. }

Web.config:

  1. <configuration>
  2. <connectionStrings >
  3. <add name="MyConStr" connectionString="server=DEBASIS;database=CSharpCorner;trusted_connection=true"/>
  4. </connectionStrings>
  5. <system.web>
  6. <compilation debug="true" targetFramework="4.5.1" />
  7. <httpRuntime targetFramework="4.5.1" />
  8. </system.web>
  9. </configuration>
Conclusion

We can use this code when we are creating registration pages like this

registration pages

Thanks for reading.