I have two dropdownlists on my page containing semester number and subject names respectively.
When I select First semester from dropdownlist1 and click on go button then the dropdownlist2 contain subjects of first semester fetched from database in sqlserver.... it is working fine, but when i select semester Second from dropdownlist1 then dropdownlist2 is containing subjects of both semesters... same problem is with other semesters.. can someone plz help me how to solve it...

public partial class studentnew : System.Web.UI.Page

    SqlConnection con = new SqlConnection(@"server= rakesh-vaio\sqlexpress; database=qms;Integrated Security=True");
    SqlCommand cmd;
    DataTable dt;
    SqlDataReader dr;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("select QuizName from QuizMaster where Semester = ' " + DropDownList1.SelectedItem.Text + "'", con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if (DropDownList1.SelectedItem.Text == "FIRST")
            {
                 DropDownList2.Items.Add(dr.GetString(0));
            }

            if (DropDownList1.SelectedItem.Text == "SECOND")
            {

             DropDownList2.Items.Add(dr.GetString(0));
            }
            if (DropDownList1.SelectedItem.Text == "THIRD")
            {
               DropDownList2.Items.Add(dr.GetString(0));
            }
            if (DropDownList1.SelectedItem.Text == "FOURTH")
            {
                DropDownList2.Items.Add(dr.GetString(0));
            }
            if (DropDownList1.SelectedItem.Text == "FIFTH")
            {
               DropDownList2.Items.Add(dr.GetString(0));
            }         
        }

        //DropDownList2.SelectedIndex = 0;
        //DropDownList1.SelectedIndex = 0;
        con.Close();
        dr.Close();
    }


    I have two dropdownlists on my page containing semester number and subject names respectively.
    When I select First semester from dropdownlist1 and click on go button then the dropdownlist2 contain subjects of first semester fetched from database in sqlserver.... it is working fine, but when i select semester Second from dropdownlist1 then dropdownlist2 is containing subjects of both semesters... same problem is with other semesters.. can someone plz help me how to solve it...

    public partial class studentnew : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"server= rakesh-vaio\sqlexpress; database=qms;Integrated Security=True");
        SqlCommand cmd;
        DataTable dt;
        SqlDataReader dr;
        SqlDataAdapter da;

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand("select QuizName from QuizMaster where Semester = ' " + DropDownList1.SelectedItem.Text + "'", con);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (DropDownList1.SelectedItem.Text == "FIRST")
                {
                     DropDownList2.Items.Add(dr.GetString(0));
                }

                if (DropDownList1.SelectedItem.Text == "SECOND")
                {

                 DropDownList2.Items.Add(dr.GetString(0));
                }
                if (DropDownList1.SelectedItem.Text == "THIRD")
                {
                   DropDownList2.Items.Add(dr.GetString(0));
                }
                if (DropDownList1.SelectedItem.Text == "FOURTH")
                {
                    DropDownList2.Items.Add(dr.GetString(0));
                }
                if (DropDownList1.SelectedItem.Text == "FIFTH")
                {
                   DropDownList2.Items.Add(dr.GetString(0));
                }         
            }

            //DropDownList2.SelectedIndex = 0;
            //DropDownList1.SelectedIndex = 0;
            con.Close();
            dr.Close();
        }
    }
}

You need to clear the 2nd dropdown before loading it.

DropDownList2.Items.Clear()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.