Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
My Code:
C#
SqlDBAccess db = new SqlDBAccess();
        protected void drpFaculty_SelectedIndexChanged(object sender, EventArgs e)
        {
            drpAdvisiorName.Items.Clear();
            drpAdvisiorName.Items.Add("<<Select Supervisor Name>>");
            try
            {
                db.Adapter("select TeacherName from tblTeacherAndDepartment where Department = '"+drpFaculty.SelectedItem.Text.ToString()+"'","teacherName");
                //lblerror.Text = drpFaculty.SelectedItem.Text.ToString();
                
                for (int i = 0; i < db.dataSet.Tables["teacherName"].Rows.Count; i++)
                {
                    //RadioButtonList1.Items.Add(db.dataSet.Tables["teacherName"].Rows[i][0].ToString());
                    drpAdvisiorName.Items.Add(db.dataSet.Tables["teacherName"].Rows[i][0].ToString());
                    //.Items.Add(db.dataSet.Tables["teacherName"].Rows[i][0].ToString());
                }

                db.dataSet.Tables["teacherName"].Clear();
            }
            catch (Exception ex)
            {
                lblerror.Text = ex.Message.ToString();
            }
        }

My ASP.NET Code:
ASP.NET
<asp:DropDownList ID="drpFaculty" runat="server"

                   style="top: 83px; left: 240px; position: absolute; height: 26px; width: 473px"
                   AutoPostBack="True" onselectedindexchanged="drpFaculty_SelectedIndexChanged">
                   <asp:ListItem>Agricultural Engineering and Technology</asp:ListItem>
                   <asp:ListItem>Veterinary Science</asp:ListItem>
                   <asp:ListItem>Agriculture</asp:ListItem>
                   <asp:ListItem>Animal Husbandry</asp:ListItem>
                   <asp:ListItem>Agricultural Economics & Rural Sociology</asp:ListItem>
                   <asp:ListItem>Fisheries</asp:ListItem>
                   <asp:ListItem>Social Science</asp:ListItem>
                   <asp:ListItem>Nutrition and Food Science</asp:ListItem>
                   <asp:ListItem>BAM</asp:ListItem>
                   <asp:ListItem>ANSVM</asp:ListItem>
                   <asp:ListItem>Arts and Humanities</asp:ListItem>
                   <asp:ListItem>Biological Science</asp:ListItem>
                   <asp:ListItem>LAW</asp:ListItem>
                   <asp:ListItem>Electrical and Computer Engineering</asp:ListItem>
                   <asp:ListItem>Mechanical</asp:ListItem>
                   <asp:ListItem>Civil Engineering</asp:ListItem>
                   <asp:ListItem>Science</asp:ListItem>
               </asp:DropDownList>


My Problem:
When I am changing in the dropdownlist items than the value is adding in another dropdownlist only 1st-time. but, when I am changing second time the value of second dropdownlist is not changing. Why this is ? Is the event fired only 1st time? Or I am using ScriptPanale thus why the problem. Please check me out.
Thank you....
Posted
Updated 7-Jul-12 21:27pm
v2
Comments
nguwar 26-Jul-12 5:13am    
um, i'm not sure whether you've already checked this link or not. It has a lot of solution regarding about "dropdownlist selectedchanged event not firing". http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/c1f66568-1ece-483c-969c-6dc66ea77c49/
Wombaticus 22-Oct-14 8:34am    
Are you quite sure? A common mistake is that the after the first time you are appending items to the second drop down without first clearing the old items - as they appear at the end, you may not have noticed them...
Richard Deeming 22-Oct-14 9:26am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Debug and see what is the issue.

1 solution

Why this line?

db.dataSet.Tables["teacherName"].Clear();
 
Share this answer
 
Comments
CHill60 23-Oct-14 10:57am    
It's over 2 years ago. I doubt the OP still has that code in that format

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