Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to programming and am trying to fill the 2nd drop down using selected value of the first. But I cant seem to the second drop down to fill with data
Can someone show me where I am going wrong?
C#
protected void Page_Load(object sender, EventArgs e)
        {
            {
                DataTable dt = new DataTable();
                OleDbConnection readData =
                    new OleDbConnection( System.Configuration.ConfigurationManager.ConnectionStrings["ExceptionReportConnection"].ConnectionString);

                readData.Open();
                var objcomand = new OleDbCommand(
                    "SELECT DISTINCT COMPANY FROM GENLIBLE.MANAGERS ORDER BY COMPANY", readData);
                objcomand.CommandType = CommandType.Text;
                OleDbDataAdapter adp = new OleDbDataAdapter(objcomand);
                adp.Fill(dt);
                readData.Close();
                ddCompany.DataSource = dt;

                ddCompany.DataValueField = "COMPANY";

                ddCompany.DataTextField = "COMPANY";

                ddCompany.DataBind();

                ddCompany.Items.Insert(0, new ListItem("--Select--", "0"));
                ddOffice.Items.Insert(0, new ListItem("--Select--", "0"));
                ddDepartment.Items.Insert(0, new ListItem("--Select--", "0"));
                ddType.Items.Insert(0, new ListItem("--Select--", "0"));

                if (ddCompany.SelectedValue == "0")
                {
                    ddOffice.Items.Clear();
                    ddOffice.Items.Insert(0, new ListItem("--Select--", "0"));
                }
            }
        }

        protected void ddCompany_OnSelectedIndexChanged(object sender, EventArgs e)
        {
                DataTable dt = new DataTable();
                var readData1 =
                    new OleDbConnection(
                        System.Configuration.ConfigurationManager.ConnectionStrings["ExceptionReportConnection"]
                            .ConnectionString);
                readData1.Open();
                ddCompany.Items.Clear();

                string s = ddOffice.SelectedValue;
                var objCommand = new OleDbCommand(
                    "SELECT DISTINCT OFFICE FROM GENLIBLE.MANAGERS " +
                    "WHERE COMPANY = '" + ddCompany.SelectedItem.Value + "'");
                
                objCommand.CommandType = CommandType.Text;
                OleDbDataAdapter adp = new OleDbDataAdapter(objCommand);
                adp.Fill(dt);
                readData1.Close();
            
                ddOffice.DataSource = dt;

                ddOffice.DataValueField = "OFFICE";

                ddOffice.DataTextField = "OFFICE";

                ddOffice.DataBind();
                ddOffice.Items.Insert(0, new ListItem("Please Select", ""));
                if (ddOffice.SelectedValue == "0")
                {
                    ddDepartment.Items.Clear();
                    ddDepartment.Items.Insert(0, new ListItem("--Select--", "0"));
                }
            }
        }
Posted
Updated 15-Oct-13 2:07am
v3
Comments
Ankur\m/ 15-Oct-13 8:02am    
It would be helpful if you tell us what exactly is not working. Do you get any error or what is it?
[no name] 15-Oct-13 8:04am    
ddCompany is filled with the data, but when i click on the next drop down there is nothing in there
Ankur\m/ 15-Oct-13 8:10am    
You mean first time when the page loads or when you change the value in 1st drop down?
[no name] 15-Oct-13 8:13am    
when the Page loads the first drop down is populated but when i select a value in the dropdown nothing happens in the second
Ankur\m/ 15-Oct-13 8:15am    
Does the page posts back? If no, you have not set AutoPostBack property to true for the 1st dropdownlist. Set it to true and it should work.
Let me know.

Based on the discussion with the OP,
1st DropDownList's AutoPostBack[^] property was not set to true. That is why nothing was happening on changing it. Setting it to true solved the issue.
 
Share this answer
 
just check autoPostback property of ddCompany
if you have not set then set it as AutoPostback="true" in aspx page

and in your page load event
write all your bussiness logic as below
C#
protected void Page_Load(object sender, EventArgs e)
        {
if(!Ispostback)
{
//your code;
}
}
 
Share this answer
 

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