Skip to content

On selecting value not change in child DropdownList

mayank prajapati

mayank prajapati

Asked Feb 18, 2015, 2:13 AM EST1 answer
Share
LinkedInX
I have created three tables CountryTable(CountryID,CountryName), StateTable(StateID(PK),StateName,CountryID(FK)),RegionTable(RegionID(PK),RegionName,StateID(FK)) 
 
 I am getting Country Name list in dropdownlist but not all other.. 
 
web.config file
_______________________ 
 
Index.aspx
 
 
Select Country:
Select State:
Select Region:
 
index.aspx.cs
 
Java
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

namespace CountryApplication
{


    public partial class Home : System.Web.UI.Page
    {
        string strConnection = ConfigurationManager.ConnectionStrings["DbContext"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindContrydropdown();
            }
        }

        protected void BindContrydropdown()
        {
            //conenction path for database
            SqlConnection con = new SqlConnection(strConnection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from CountryTable", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddlCountry.DataSource = ds;
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryID";
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
            ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
            ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));

        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
            SqlConnection con = new SqlConnection(strConnection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from StateTable where CountryID=" + CountryID, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddlState.DataSource = ds;
            ddlState.DataTextField = "StateName";
            ddlState.DataValueField = "StateID";
            ddlState.DataBind();
            ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
            if (ddlState.SelectedValue == "0")
            {
                ddlRegion.Items.Clear();
                ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
            }
        }

        protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
        {
            int StateID = Convert.ToInt32(ddlState.SelectedValue);
            SqlConnection con = new SqlConnection(strConnection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from RegionTable where StateID=" + StateID, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddlRegion.DataSource = ds;
            ddlRegion.DataTextField = "RegionName";
            ddlRegion.DataValueField = "RegionID";
            ddlRegion.DataBind();
            ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        }
}
} 
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    you are using dataset to filled the state dropdownlist in ddlcountry_selectedIndexChanged. while using dataset as datasource you have to give the table number also. use like this : ddlState.DataSource = ds.Tables[0]; Check whether from this its binding or not
    +1