1. Load the "DropDownList1"
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack){
using (SqlConnection oConn = new SqlConnection()){
oConn.ConnectionString = "data source=localhost;initial catalog=test_db;user=sa;password=123";
SqlDataAdapter oda = new SqlDataAdapter("select distinct year from Acadamics", oConn);
DataSet ds = new DataSet();
oda.Fill(ds);
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataValueField = "year";
DropDownList1.DataTextField = "year";
DropDownList1.DataBind();
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
2. Change the
"DropDownList1"
property to
autopostback="true"
3. Populate the grid on the DropdownList selected index changed event.
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
try
{
using (SqlConnection oConn = new SqlConnection())
{
oConn.ConnectionString = "data source=localhost;initial catalog=test_db;user=sa;password=123";
SqlDataAdapter oda = new SqlDataAdapter("select * from Acadamics where year=" + DropDownList1.SelectedValue.ToString(), oConn);
DataSet ds = new DataSet();
oda.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
try to pass your combo box value to the grid population method using parameters to avoid any SQL injection.