Hi, I have 2 dropdownlists on a page. (DDL1 and DDL2). The DDL1 is populated by values from the database using a table1. DDL2 is dependent on DDL1. As soon as a value is selected in DDL1 I need to populate values in DDL2, though using S table2 which is also available on the page. I need to know how I can populate DDL2 only after a value is selected in DDL1. This might sound as a redundant question though I never found a post that will help me achieve this using database(sqlserver).

I need help in asp

Thanks for your help in advance.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{

    SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=student;Integrated Security=True");
    SqlCommand cmd;
    SqlDataReader dr, dr1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillcmb1();
        }
        
    }
    void fillcmb1()
    {
        DropDownList1.Items.Clear();
        cnn.Open();
        cmd = new SqlCommand("select distinct(class) from table_1", cnn);
        dr1 = cmd.ExecuteReader();
        while (dr1.Read())
        {
            DropDownList1.Items.Add(System.Convert.ToString(dr1[0]));
        }
        dr1.Close();
        cnn.Close();

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        cnn.Open();
        DropDownList2.Items.Clear();
        cmd = new SqlCommand("select name from table_1 where class like '" + DropDownList1.SelectedItem.Text + "%'", cnn);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            DropDownList2.Items.Add(System.Convert.ToString(dr["name"]));
        }
        dr.Close();
        cnn.Close();

        //fillcmb1();
    }
    
}
Be a part of the DaniWeb community

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