Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I Am Using Cascading Drop down In Asp.Net Website But As I Select Third Or Forth Item From Drop down And Save It, It Stores First Value Only. I filled my form and select Country from country drop down it works smoothly but as i select select country accordingly state drop down is populated and as i select state and click on submit button. Country selected item is stored as per selection but state drop down always selects first item in the list.

protected void Page_Load(object sender, EventArgs e)
{
FirstName.Focus();
if (!IsPostBack)
{
FillCountry();

}
public void FillStates()
{
cn.Open();
cmd.Connection = cn;
cmd.CommandText = "Select CountryID, States from States where CountryId = '" + Country.SelectedValue + "'";
SqlDataReader dr = cmd.ExecuteReader();
State.Items.Clear();
State.DataSource = dr;
State.DataTextField = "States";
State.DataValueField = "CountryID";
State.DataBind();
State.Items.Insert(0, "--Select--");
cn.Close();
cmd.Dispose();
}
protected void Country_SelectedIndexChanged(object sender, EventArgs e)
{
FillStates();
Posted
Updated 24-Mar-15 20:33pm
v3
Comments
Thanks7872 25-Mar-15 1:24am    
Use google. I suggest you to learn some basic ASP.NET first.
Member 11478531 25-Mar-15 2:23am    
i am building a web application of data insert into database using asp.net C# so there i have two dropdown list and when i select item from first dropdown proper selection and insertion happens but as i select from second drop down which is populated according to first drop down ( Cascading DropDown ) on postback second dropdown selects first item instead of selected this is my actual problem.
Thanks7872 25-Mar-15 2:26am    
Then use 'improve question' link at bottom of your question to edit it. Post this explanation there. Show smallest part of problematic code you have implemented to achieve the same.
Member 11478531 25-Mar-15 2:34am    
can you please now check and let me know
Member 11478531 25-Mar-15 7:09am    
????

This is to be done just like you would save a standard textbox value into the database.
For standard tutorials on saving to database, go through -
Save data from textboxes to database in asp.net [^]
How to Insert data into database in asp net [^]

For specific example on dropdowns -
Save multiple selected list items in database from dropdown list when selected[^]
 
Share this answer
 
v3
Comments
Member 11478531 25-Mar-15 2:32am    
Thanks for the reply but i have improved my Question
protected void Page_Init(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            FillCountry();
           
        }
    }
 public void FillCountry()
    {
        cn.Open();
        cmd.Connection = cn;
        cmd.CommandText = "Select ID, Countries from Countries";
        SqlDataReader dr = cmd.ExecuteReader();
        Country.Items.Clear();
        Country.DataSource = dr;
        Country.DataTextField = "Countries";
        Country.DataValueField = "ID";
        Country.DataBind();
        Country.Items.Insert(0, "--Select--");
        cn.Close();
        cmd.Dispose();
    }

    public void FillStates()
    {

        if (Page.IsPostBack)
        {
            cn.Open();
            cmd.Connection = cn;
            cmd.CommandText = "Select StateID, States from States where CountryId = '" + Country.SelectedValue + "'";
            SqlDataReader dr = cmd.ExecuteReader();
            State.Items.Clear();
            State.DataSource = dr;
            State.DataTextField = "States";
            State.DataValueField = "StateID";
            State.DataBind();
            State.Items.Insert(0, "--Select--");
            cn.Close();
            cmd.Dispose();

        }
    }



    protected void Country_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillStates();
    }
}
 
Share this answer
 
Comments
Member 11478531 25-Mar-15 8:08am    
FillCountry () can be used in page_Load but you need to not is postback Check has to be done ..

your States Table Should have 3 columns ..

StateID States CountryID

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