Click here to Skip to main content
15,300,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends.. I am doing a small project based on Drop Down List. I have two tables, State & Country. I have two Dropdowns ddlState, ddlCountry. When i select a particular state in ddlState, the country of that state should appear in ddlCountry..

(I can able to do other way round.. like, I can select a country from ddlCountry & the states of that country are getting displayed in ddlState)
please help me
Posted
Comments
Suvendu Shekhar Giri 15-Dec-15 5:09am
   
This should be an easy stuff to do.
In the ddlState SelectedIndexChanged event set the SelectedValue property of ddlCountry with the value from your State table which stores countryname/countryid.

Share the relevant code.
Member 11933161 15-Dec-15 5:17am
   
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int M_STATE_SLNO = Convert.ToInt16(ddlState.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from M_COUNTRY where M_COUNTRY_CODE=" +M_STATE_SLNO, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "M_COUNTRY_NAME";
ddlCountry.DataValueField = "M_COUNTRY_CODE";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlCountry_SelectedIndexChanged(null, null);

}
F-ES Sitecore 15-Dec-15 5:16am
   
On the changed event of the state dropdown get the selected state and retrieve the county that state is in (how you do that depends on your database schema and what database access technology you're using, which you haven't detailed). You should now have the data you need to populate the country dropdown. You might additionally need to load all countries to populate the country drop down, or you might also then want to retrieve all states in the selected country too. You haven't really explained the business logic fully.

If you're done it one way you should be able to do it the other so you might have to paste the code you're trying.
Member 11933161 15-Dec-15 5:16am
   
Actually i did the same..But the out put was something else..
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int M_STATE_SLNO = Convert.ToInt16(ddlState.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from M_COUNTRY where M_COUNTRY_CODE=" +M_STATE_SLNO, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "M_COUNTRY_NAME";
ddlCountry.DataValueField = "M_COUNTRY_CODE";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlCountry_SelectedIndexChanged(null, null);

}

1 solution

You don't need to call the event handler of ddlCountry, that may reset the selection in State dropdown. Further you seem to have other problems too.
You are trying to filter country by stateid.
Check this-
C#
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
   int M_STATE_SLNO = Convert.ToInt16(ddlState.SelectedValue);
   SqlConnection con = new SqlConnection(strConnection);
   con.Open();
   SqlCommand cmd = new SqlCommand("select * from M_COUNTRY where M_COUNTRY_CODE=(SELECT M_COUNTRY_CODE FROM M_STATE WHERE M_STATE_CODE="+M_STATE_SLNO+")", con); //Assuming coulmns names and table names as such for State table
   SqlDataAdapter da = new SqlDataAdapter(cmd);
   DataSet ds = new DataSet();
   da.Fill(ds);
   con.Close();
   ddlCountry.DataSource = ds;
   ddlCountry.DataTextField = "M_COUNTRY_NAME";
   ddlCountry.DataValueField = "M_COUNTRY_CODE";
   ddlCountry.DataBind();
   //ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
   //ddlCountry_SelectedIndexChanged(null, null);
}


Hope, it helps :)
   
Comments
Member 11933161 15-Dec-15 5:53am
   
@Suvendu Shekhar Giri Thank You So Much sir.. The issue is Resolved....
Suvendu Shekhar Giri 15-Dec-15 6:05am
   
Most welcome. Glad that it helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900