Filtering a DropDownList through another

shahab

Junior Contributor
Joined
Aug 14, 2003
Messages
206
Location
Iran(Middle East)
Code:
void DDL1Binder()
		{
			
			string strCon = ConfigurationSettings.AppSettings["ConnStr"]; 
			SqlConnection wg = new SqlConnection(strCon); 
			
			SqlDataAdapter dtAdap1 = new  
				SqlDataAdapter("select major from tblQCategory ",wg); 
			  
			dtAdap1.Fill(ds1, "tblQCategory");				
			DropDownList1.DataSource = ds1.Tables["tblQCategory"].DefaultView;
			DropDownList1.DataBind();
	
		}
//--------------------------------------------------------------------------
		void DDL2Binder()
		{
			
			string strCon = ConfigurationSettings.AppSettings["ConnStr"]; 
			SqlConnection wg = new SqlConnection(strCon); 
			
			SqlDataAdapter dtAdap2 = new  
				SqlDataAdapter("select lesson from tblQCategory "
				//+" WHERE major = '" + DropDownList1.SelectedItem.Text.ToString() +"'",wg); 
				//+" WHERE major = '" + DropDownList1.DataTextField.ToString()+"'",wg); False
				//+" WHERE major = '" + DropDownList1.SelectedItem.Text.Trim() +"'",wg); False
			  //+" WHERE major = N'ادبیات'",wg); True
			dtAdap2.Fill(ds2, "tblQCategory");				
			DropDownList2.DataSource = ds2.Tables["tblQCategory"].DefaultView;
			DropDownList2.DataBind();
	
		}

		private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			//DDL2Binder();
		}
What should I write instead of this? :confused:
+" WHERE major = '" + DropDownList1.SelectedItem.Text.ToString() +"'",wg);
 
Make sure your type are good so that you don't add a ' and make an error of type.
Can't you filter your DefaultView instead ? make less problem

Hint: If you use stored proc, it's more adaptable as solution.
 
What error are you getting?

PS: if your not using visual studio you will have to add your own event handler...
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
 
Just do a simple

dv.RowFilter = "major=" + DropDownList1.SelectedValue;

and you set DataSource of your DDL to dv
and you just do a little DataBind()
 
Back
Top