You must Sign In to post a response.
  • Category: ASP.NET

    How get country names as drop down list selected items

    query to create and insert countries names as base table and states in the sub-table and cities in the sub-sub table so that i can get values in the dropdown listitems as country in count after selectin of country states availble and in the states available cities.
  • #648180
    Hello,


    please refer below site:
    http://www.codeproject.com/KB/aspnet/CitiesDropDownList.aspx


    This article aims to show how to create 3 dropdownlists to display countries, states and cities.
    When you select a country in the countries dropdownlist, the states dropdownlist will fetch the related states for that country.

    Then when you select a state, the cities dropdownlist will fetch the related cities for that state.


    Regards
    Rajesh

    Regards
    Mr.Rajesh
    Software Engg.

  • #648198
    Hi rajeswari,
    first you can bind country name to first dropdown,in this we need country id and name,datatextfieid is country name,datavaluefield is country id,when we select coutry name,we can take country id from the selectedindexchanged event of dropdown list,now we can send country id to database,now we can retrive state name with ids,now we find state name to second dropdownlist ,similarly in city

  • #648215
    Hi,

    You may use Ajax Script manager to avoid full post back.
    Please check below code,


    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True" onselectedindexchanged="ddlCountry_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="True">
    </asp:DropDownList>
    </ContentTemplate>
    </asp:UpdatePanel>


    And in your codebehind,


    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    PopulateCountry();
    }
    }


    public void PopulateCountry()
    {
    string strCon = ConfigurationManager.ConnectionStrings["CConnectionString"].ToString();
    SqlConnection conn = new SqlConnection(strCon);
    conn.Open();
    SqlDataAdapter da = null;
    DataSet dsCountry = new DataSet();
    try
    {
    string strCountrySQL = "SELECT CountryName, CountryCode FROM tblCountry";
    da = new SqlDataAdapter(strCountrySQL, conn);
    da.Fill(dsCountry);
    ddlCountry.DataSource = dsCountry;
    ddlCountry.DataTextField = "CountryName";
    ddlCountry.DataValueField = "CountryCode";
    ddlCountry.DataBind();
    ddlCountry.Items.Insert(0, new ListItem("--Select--", "--Select--"));
    ddlCountry.SelectedItem.Selected = false;
    ddlCountry.Items.FindByText("--Select--").Selected = true;
    }
    catch (Exception ex)
    {
    lblError.Text = "Error!! <br>+" + ex.Message.ToString();
    }
    finally
    {
    dsCountry.Dispose();
    da.Dispose();
    conn.Close();
    conn.Dispose();
    }
    }

    private void PupulateState(string strCountry)
    {
    string strCon = ConfigurationManager.ConnectionStrings["CConnectionString"].ToString();
    SqlConnection conn = new SqlConnection(strCon);
    conn.Open();
    SqlDataAdapter da = null;
    DataSet ds = new DataSet();
    try
    {
    string strSql = "SELECT StateCode, State FROM tblState WHERE CountryCode = '" + strCountry + "'";
    da = new SqlDataAdapter(strSql, conn);
    da.Fill(ds, "State");

    ddlState.DataSource = ds.Tables["State"].DefaultView;
    ddlState.DataTextField = "StateName";
    ddlState.DataValueField = "StateCode";
    ddlState.DataBind();
    ddlState.Items.Insert(0, new ListItem("--Select--", "--Select--"));
    ddlState.SelectedItem.Selected = false;
    ddlState.Items.FindByText("--Select--").Selected = true;

    }
    catch (Exception ex)
    {
    lblError.Text = "Error!! <br>+" + ex.Message.ToString();
    }
    finally
    {
    ds.Dispose();
    da.Dispose();
    conn.Close();
    conn.Dispose();
    }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
    string strCountry = ddlCountry.SelectedValue.ToString();
    PupulateState(strCountry);
    }


    Similarly you may add another dropdown for Cities in the state.


    Regards,
    Asheej T K

  • #648292
    hi try this code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using BOclass;
    using BALclass;
    using DALclass;

    public partial class admin_postproduct : System.Web.UI.Page
    {

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    showproductcategory();
    MultiView1.ActiveViewIndex = 0;
    }
    }
    #endregion

    #region show sub-category
    public void showproductsubcategory()
    {
    bosubcatshow boinfo = new bosubcatshow();
    balsubcatshow balinfo = new balsubcatshow();
    //boinfo.catname = ddlproductcat.Text;
    boinfo.catname = ddlproductcat.SelectedValue;

    DataSet ds = new DataSet();
    ds = balinfo.balshow(boinfo);
    ddlsubcat.DataSource = ds;
    ddlsubcat.DataTextField = "sub_name";
    ddlsubcat.DataValueField = "sub_name";
    ddlsubcat.DataBind();
    ddlsubcat.Items.Insert(0, new ListItem("Select", ""));

    }
    #endregion

    #region show category
    public void showproductcategory()
    {
    dalsubcat dalinfo = new dalsubcat();
    DataSet ds = new DataSet();
    ds = dalinfo.dalddlcat();
    ddlproductcat.DataSource = ds;
    ddlproductcat.DataTextField="cat_name";
    ddlproductcat.DataValueField = "cat_name";
    ddlproductcat.DataBind();
    ddlproductcat.Items.Insert(0, new ListItem("Select", ""));


    }
    #endregion


    protected void btnaddcat_Click(object sender, EventArgs e)
    {
    MultiView1.ActiveViewIndex = 2;

    }
    protected void btnaddsubcat_Click(object sender, EventArgs e)
    {
    MultiView1.ActiveViewIndex = 3;
    lblcatname.Text = ddlproductcat.Text;
    }

    #region Add Product
    protected void btnpadd_Click(object sender, EventArgs e)
    {
    //MultiView1.ActiveViewIndex = 1;
    balpostproduct balinfo = new balpostproduct();
    bopostproduct boinfo = new bopostproduct();
    try
    {
    boinfo.category = ddlproductcat.Text;
    boinfo.subcategory = ddlsubcat.Text;
    boinfo.manufacture = ddbrand.Text;
    boinfo.producttype = txtptype.Text;
    boinfo.productname = txtpname.Text;
    boinfo.productsrial = txtproductserial.Text;
    boinfo.saleprice = txtpsaleprice.Text;
    boinfo.oldprice = txtpoldprice.Text;
    boinfo.shortdesc = txtpshortdesc.Text;
    boinfo.productimage = fuproduct.FileName.ToString();
    fuproduct.PostedFile.SaveAs(Request.PhysicalApplicationPath + @"\images\" + boinfo.productimage);
    boinfo.outputs = 0;
    int rt = balinfo.balproduct(boinfo);
    if (rt == 1)
    {
    lblproductmsg.Text = "Product inserted Successfully...........";
    txtptype.Text = ""; txtpname.Text = ""; txtproductserial.Text = ""; txtpsaleprice.Text = ""; txtpoldprice.Text = "";
    txtpshortdesc.Text = "";
    }
    else if (rt == -1)
    {
    lblproductmsg.Text = "Fail to insert Try Again..........";
    }
    }
    catch (Exception p)
    {
    lblproductmsg.Text = p.Message.ToString();
    }

    }
    #endregion

    #region Add Category
    protected void Button1_Click(object sender, EventArgs e)
    {
    try
    {
    baladdcat balinfo = new baladdcat();
    boaddcat boinfo = new boaddcat();
    boinfo.cat_name = txtcname.Text;
    boinfo.cat_desc = txtcatdes.Text;
    boinfo.cat_image = fucat.FileName.ToString();
    fucat.PostedFile.SaveAs(Request.PhysicalApplicationPath + @"\catimages\" + boinfo.cat_image);
    boinfo.outputs = 0;
    int rt = balinfo.balcat(boinfo);
    if (rt == -1)
    {
    Label20.Text = "Category Name Already Present.........";
    txtcname.Text = "";
    txtcatdes.Text = "";
    }
    else
    {
    if (rt == 1)
    {
    Label20.Text = "Category Added Sucessfully.......";
    txtcname.Text = "";
    txtcatdes.Text = "";
    }
    else
    {
    Label20.Text = "Fail to Add Category..........";
    txtcname.Text = "";
    txtcatdes.Text = "";
    }
    }
    MultiView1.ActiveViewIndex = 2;
    }
    catch (Exception ab)
    {
    Label20.Text = ab.Message.ToString();
    }

    }
    #endregion

    #region Add Sub-category
    protected void btnsubcat_Click(object sender, EventArgs e)
    {
    try
    {
    balsubcat balinfo = new balsubcat();
    boaddsubcat boinfo = new boaddsubcat();
    boinfo.s_catname = lblcatname.Text;
    boinfo.s_subcatname = txtsubcat.Text;
    boinfo.s_desc = txtdesc.Text;
    boinfo.s_image = fusubcatimage.FileName.ToString();
    fusubcatimage.PostedFile.SaveAs(Request.PhysicalApplicationPath + @"\subcatimages\" + boinfo.s_image);
    boinfo.outputs = 0;
    int rt = balinfo.balsub(boinfo);
    if (rt == -1)
    {
    Label21.Text = "Sub-Category Name Already Present.........";
    txtsubcat.Text = "";
    txtdesc.Text = "";
    }
    else
    {
    if (rt == 1)
    {
    Label21.Text = "Sub-Category Added Sucessfully.......";
    txtsubcat.Text = "";
    txtdesc.Text = "";
    }
    else
    {
    Label21.Text = "Fail to Add Category..........";
    txtsubcat.Text = "";
    txtdesc.Text = "";
    }

    MultiView1.ActiveViewIndex = 3;
    }
    }
    catch(Exception abc)
    {
    Label21.Text=abc.Message.ToString();
    }


    }
    #endregion

    #region show sub-cat
    protected void ddlproductcat_selectindexchange(object sender, EventArgs e)
    {
    showproductsubcategory();
    }
    #endregion

    protected void btnpcancel_Click(object sender, EventArgs e)
    {
    txtptype.Text = ""; txtpname.Text = ""; txtproductserial.Text = ""; txtpsaleprice.Text = ""; txtpoldprice.Text = "";
    txtpshortdesc.Text = "";
    ddlproductcat.SelectedIndex = 0;
    ddlsubcat.SelectedIndex = 0;
    ddbrand.SelectedIndex = 0;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    txtcname.Text = ""; txtcatdes.Text = "";
    MultiView1.ActiveViewIndex = 0;
    }

    protected void btncancelsub_Click(object sender, EventArgs e)
    {
    txtsubcat.Text = ""; txtdesc.Text = "";
    MultiView1.ActiveViewIndex = 0;
    }

    protected void ddlsubcat_SelectedIndexChanged(object sender, EventArgs e)
    {
    showbrand();
    }


    #region Show Brand
    public void showbrand()
    {
    bobrand boinfo = new bobrand();
    balbrand balinfo = new balbrand();
    boinfo.cat = ddlproductcat.SelectedValue;
    boinfo.subcat = ddlsubcat.SelectedValue;

    DataSet ds = new DataSet();
    ds = balinfo.balbrandshow(boinfo);
    ddbrand.DataSource = ds;
    ddbrand.DataTextField = "b_name";
    ddbrand.DataValueField = "b_name";
    ddbrand.DataBind();
    ddbrand.Items.Insert(0, new ListItem("Select", ""));


    }
    #endregion

    protected void Button3_Click(object sender, EventArgs e)
    {

    lblbcat.Text = ddlproductcat.Text;
    lblbsubcat.Text = ddlsubcat.Text;
    MultiView1.ActiveViewIndex = 1;
    }

    #region add brand
    protected void Button4_Click(object sender, EventArgs e)
    {
    baladdbrand balinfo = new baladdbrand();
    boaddbrand boinfo = new boaddbrand();
    try
    {
    boinfo.cat = lblbcat.Text;
    boinfo.subcat = lblbsubcat.Text;
    boinfo.brand = txtbrand.Text;
    boinfo.outputs = 0;
    int rt = balinfo.balbrand(boinfo);
    if (rt==-1)
    {
    lblmsgbrand.Text = "Brand Name Already Present.........";
    txtbrand.Text = "";
    }
    else
    {
    if (rt == 1)
    {
    lblmsgbrand.Text = "Category Added Sucessfully.......";
    txtbrand.Text = "";
    }
    else
    {
    lblmsgbrand.Text = "Fail to Add Category..........";
    txtbrand.Text = "";
    }
    }
    }
    catch (Exception b)
    {
    lblmsgbrand.Text = b.Message.ToString();
    }

    }
    #endregion

    protected void Button5_Click(object sender, EventArgs e)
    {
    MultiView1.ActiveViewIndex = 0;
    }
    }








    selectindex change of country
    code for show state

    Thanks

  • #648441
    Hai Rajeswari,

    you can create a method which will be used to fill the drop-down lists and then call that method.

    see the code snippet of the method as below:


    private void FillDropdownList(string dropdownList)
    {
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection con = new SqlConnection(connectionString);
    SqlDataAdapter da = null;
    string strSql = string.Empty;
    DataSet ds = new DataSet();
    try
    {
    switch (dropdownList)
    {
    case "Country":
    strSql = "SELECT COUNTRYNAME FROM COUNTRY";
    break;
    case "State":
    strSql = "SELECT STATENAME FROM STATE WHERE COUNTRYID=" + ddlCountry.SelectedItem.Text;
    break;
    case "City":
    strSql = "SELECT CITYNAME FROM CITY WHERE STATEID=" + ddlState.SelectedItem.Text;
    break;
    default:
    break;
    }
    da = new SqlDataAdapter(strSql, con);
    da.Fill(ds, "Table");
    if (ds.Tables[0].Rows.Count > 0)
    {
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    switch (dropdownList)
    {
    case "Country":
    ddlCountry.Items.Add(ds.Tables[0].Rows[0][0].ToString());
    break;
    case "State":
    ddlState.Items.Add(ds.Tables[0].Rows[0][0].ToString());
    break;
    case "City":
    ddlCity.Items.Add(ds.Tables[0].Rows[0][0].ToString());
    break;
    default:
    break;
    }
    }
    }

    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    finally
    {
    ds.Dispose();
    da.Dispose();
    con.Dispose();
    }
    }


    Noe call the method for the country at the page_load event as :


    protected void Page_Load(object sender, EventArgs e)
    {
    FillDropdownList("Country");
    }

    To fill the state, call the same method for the country dropdown list selected indexed change as:


    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
    FillDropdownList("State");
    }

    To fill the cities, call the same method in state drop-down list selected index changed event as :


    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
    FillDropdownList("City");
    }

    Hope it will help.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #648553
    Use below code for this:

    protected void Page_Load(object sender, EventArgs e)
    {

    if (!Page.IsPostBack)
    {
    CountryBind();
    }
    }
    }
    protected void CountryBind()
    {
    string sqlQuery = "Select * from Country";

    SqlConnection conn = new SqlConnection(c);

    conn.Open();
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
    da.Fill(ds);
    ddl_country.DataSource = ds;
    ddl_country.DataBind();
    conn.Close();

    }
    protected void StateBind()
    {
    string sqlQuery = "SELECT * FROM State " + " WHERE (CountryID = " + Convert.ToInt32(ddl_country.SelectedValue) + " )";

    SqlConnection conn = new SqlConnection(c);
    {
    conn.Open();
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
    da.Fill(ds);
    ddl_state.DataSource = ds;
    ddl_state.DataBind();
    conn.Close();
    }
    }
    protected void CityBind()
    {
    string sqlQuery = "SELECT * FROM City " + " WHERE (StateID = " + Convert.ToInt32(ddl_state.SelectedValue) + " and CountryID=" + Convert.ToInt32(ddl_country.SelectedValue)+")";

    SqlConnection conn = new SqlConnection(c);
    {
    conn.Open();
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
    da.Fill(ds);
    ddl_city.DataSource = ds;
    ddl_city.DataBind();
    conn.Close();
    }
    }
    protected void ddl_country_SelectedIndexChanged(object sender, EventArgs e)
    {
    StateBind();
    }
    protected void ddl_state_SelectedIndexChanged(object sender, EventArgs e)
    {
    CityBind();
    }

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.